使用XCode故事板实例化使用XIB进行设计的视图控制器 [英] Using XCode storyboard to instantiate view controller that uses XIB for its design

查看:156
本文介绍了使用XCode故事板实例化使用XIB进行设计的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个先前创建的UIViewController子类,其子控件放置在XIB文件中.

我现在想在情节提要中使用此视图控制器,但似乎尽管我可以在情节提要中指示视图控制器的类名,但我无法告诉它加载XIB.

我不想将所有内容从XIB移到情节提要,而是将其保存在单独的XIB中.

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

解决方案

  1. 删除情节提要中视图控制器所包含的视图.

  2. 然后通过配置具有相同名称的笔尖文件提供视图 作为视图控制器类.例如,如果视图控制器 该类称为MyViewController,命名您的xib文件 MyViewController.xib.

编辑请注意,Swift种子5开始打破了这项技术,因为它使正在寻找的.xib文件名变得混乱.解决方法请参阅我的答案: https://stackoverflow.com/a/25539016/341994 基本上,此名称匹配为在iOS 8中损坏,但随后苹果对其进行了悔改并进行了修复,以使其在iOS 9中再次运行.

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?

解决方案

  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.

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.

这篇关于使用XCode故事板实例化使用XIB进行设计的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆