使用 XCode 故事板实例化使用 XIB 进行设计的视图控制器
我之前创建了一个 UIViewController 子类,它的控件布局在 XIB 文件中。
我现在想在故事板中使用这个视图控制器,但似乎虽然我可以在故事板中指示视图控制器的类名,但我无法告诉它加载 XIB。
我不想将 XIB 中的所有内容移至故事板,而是将其保留在单独的 XIB 中。
如何在故事板中获取此 UIViewController 来加载我的 XIB?
I have a UIViewController subclass that I created previously that had its controls laid out in a XIB file.
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.
I'd rather not move everything from the XIB to the storyboard but keep it in its separate XIB.
How can I get this UIViewController in the storyboard to load my XIB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除故事板中视图控制器包含的视图。
然后通过配置同名的 nib 文件来提供视图
作为视图控制器类。例如,如果视图控制器
类名为 MyViewController,将您的 xib 文件命名为
MyViewController.xib。
编辑 请注意,Swift Seed 5 开始破坏此技术,因为它破坏了它正在查找的 .xib 文件的名称。请参阅我的答案以获取解决方法: https://stackoverflow.com/a/25539016/341994 基本上这个名称匹配是<在 iOS 8 中出现了问题,但后来 Apple 后悔并修复了它,以便在 iOS 9 中再次正常工作。
Delete the View contained by the view controller in the storyboard.
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.
EDIT Note 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/341994 Basically this name matching was broken in iOS 8, but then Apple repented and fixed it so that it works again in iOS 9.