加载 UIViewController “from"笔尖文件 [英] Loading UIViewController "from" Nib File
问题描述
我正在尝试从 Nib 文件创建一个 UIViewController
.在 Google 上,我发现我只能从 Nib 加载 UIView.
I am trying to create an UIViewController
from a Nib file. On Google I found that I can only load an UIView from Nib.
但有些人建议我可以创建一个 Nib 文件(UIView 的),其文件所有者设置为我们的 ViewController
.
But some suggests that I could create a Nib file (of UIView) whose File Owner is set to our ViewController
.
这就是我所做的,我没有崩溃,但只是没有显示视图.
That is what I did, I got no crashes, but the View is just not displayed.
尝试像这样推送 viewcontroller
self.navigationController!.pushViewController(CoolViewController(), animated: true );
但是按了还是黑屏
XCode 6.3 - 不使用故事板
XCode 6.3 - Not using Storyboards
推荐答案
你需要分配你的ViewController
,然后通过告诉iOS笔尖的名字来初始化它.
You need to allocate your ViewController
, then initialize it by telling the iOS the name of the nib.
我看到你在使用 Swift;恐怕我不知道swift,只知道objective-c.但这是在objective-c中完成的方式:
I see you're using Swift; I'm afraid I don't know swift, only objective-c. But here is how it would be done in objective-c:
[self.navigationController pushViewController [[[CoolViewController alloc] initWithNibName: @"CoolDesign" bundle: nil] autorelease];
... 其中CoolDesign"是您笔尖的基本名称.也就是说,你在 Interface Builder 中创建 CoolDesign.xib,Xcode 将 XML - text - xib 编译成 CoolDesign.nib,然后你告诉 initWithNibName 打开 @"CoolDesign".
... where "CoolDesign" is the base name of your nib. That is, you create CoolDesign.xib in Interface Builder, Xcode compiles the XML - text - xib into CoolDesign.nib, then you tell initWithNibName to open just @"CoolDesign".
仅仅告诉 Interface Builder 设计文档是一个 UIViewController
是不够的.虽然原则上 iOS 可以弄清楚你的意思,但原则上你可以为单个 UIViewController
子类使用多个笔尖.
It's not enough just to tell Interface Builder that a design document is a UIViewController
. While in principle the iOS could figure out what you mean, also in principle you could have multiple nibs for a single UIViewController
subclass.
这篇关于加载 UIViewController “from"笔尖文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!