使用 XCode 故事板实例化使用 XIB 进行设计的视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9155719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:13:25  来源:igfitidea点击:

Using XCode storyboard to instantiate view controller that uses XIB for its design

xcodestoryboardxibnib

提问by user1175914

I have a UIViewController subclass that I created previously that had its controls laid out in a XIB file.

我有一个之前创建的 UIViewController 子类,它的控件布局在 XIB 文件中。

I'd like to use this view controller in a storyboard now, but it seems although I can indicate a view controller's classname in the storyboard, I can't tell it to load the XIB.

我现在想在情节提要中使用这个视图控制器,但似乎虽然我可以在情节提要中指明视图控制器的类名,但我无法告诉它加载 XIB。

I'd rather not move everything from the XIB to the storyboard but keep it in its separate XIB.

我宁愿不将所有内容从 XIB 移动到故事板,而是将其保留在单独的 XIB 中。

How can I get this UIViewController in the storyboard to load my XIB?

如何在情节提要中获取此 UIViewController 以加载我的 XIB?

回答by matt

  1. Delete the View contained by the view controller in the storyboard.

  2. Then provide the view by configuring a nib file with the same name as the view controller class. For example if the view controller class is called MyViewController, name your xib file MyViewController.xib.

  1. 删除故事板中视图控制器包含的视图。

  2. 然后通过配置一个与视图控制器类同名的 nib 文件来提供视图。例如,如果视图控制器类名为 MyViewController,请将您的 xib 文件命名为 MyViewController.xib。

EDITNote that Swift seed 5 started breaking this technique, because it mangles the name of the .xib file it's looking for. See my answer here for workarounds: https://stackoverflow.com/a/25539016/341994Basically this name matching was broken in iOS 8, but then Apple repented and fixed it so that it works again in iOS 9.

编辑请注意,Swift 种子 5 开始破坏这种技术,因为它破坏了它正在寻找的 .xib 文件的名称。有关解决方法,请在此处查看我的答案:https: //stackoverflow.com/a/25539016/341994基本上,此名称匹配在 iOS 8 中损坏,但随后 Apple 后悔并修复了它,以便它在 iOS 9 中再次运行