问题描述
我正在尝试从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
.
这就是我所做的,我没有崩溃,但是View没有显示。
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;我害怕我不知道快速,只有客观 - 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
子类提供多个nib。
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“来自”笔尖文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!