抱歉,伙计们,可能是菜鸟问题。我是 iOS 开发新手。
如何创建在 XIB 中定义的 View Controller ?
当您通过“新建文件”在 Xcode 中创建继承自 UIViewController
的新类并标记 with XIB
复选框时,它会创建没有 viewController 的 xib 文件
元素。而是它有 placeholder
指向 UIViewController
,在代码中定义。
但我看到您可以在 Interface Builder 中创建 Controller 本身并为其指定 customClass。
不幸的是,它对我不起作用。我在运行时遇到 loaded the "EmbeddedViewController"nib but the view outlet was not set
错误。但我相信 view
属性必须由资源加载器自动设置,因为所有内容都是通过 XIB 定义的。例如,对于 TableView 及其单元格,它就是这样工作的。
能否举例说明如何操作?
我创建了示例项目:https://github.com/cppexpert/SampleWithNib
有问题的 Controller 是 EmbeddedViewController
最佳答案
为每个 UIViewController 创建一个 Xib
每个 Xib 都有 File's Owner 对象供您在那里设置 UIViewController 类。单击 File's Owner 并在其类上选择 EmbeddedViewController
然后将主 UIView 而不是 UIViewController 类拖到那里,然后将此 View 与文件所有者 Hook 作为 View 。 UIViewController 仅用于拖动到 StoryBoard 基础项目。
https://github.com/lequysang/gitfiles02/blob/master/SampleWithNib-master.zip
关于ios - 如何创建在 XIB 中定义的 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18523645/