ios 从 Nib 文件加载 UIViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30316257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading UIViewController "from" Nib File
提问by JayVDiyk
I am trying to create an UIViewController
from a Nib file. On Google I found that I can only load an UIView from Nib.
我正在尝试UIViewController
从 Nib 文件创建一个。在 Google 上,我发现我只能从 Nib 加载 UIView。
But some suggests that I could create a Nib file (of UIView) whose File Owner is set to our ViewController
.
但有人建议我可以创建一个 Nib 文件(UIView 的),其文件所有者设置为我们的ViewController
.
That is what I did, I got no crashes, but the View is just not displayed.
这就是我所做的,我没有崩溃,但是没有显示视图。
EDIT:
Tried to push the viewcontroller
like this
编辑:试图推动viewcontroller
这样
self.navigationController!.pushViewController(CoolViewController(), animated: true );
But it still showing black screen when it is pushed
但是按了还是黑屏
XCode 6.3 - Not using Storyboards
XCode 6.3 - 不使用故事板
采纳答案by Mike Crawford
You need to allocate your ViewController
, then initialize it by telling the iOS the name of the nib.
您需要分配您的ViewController
,然后通过告诉 iOS 笔尖的名称来初始化它。
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:
我看到你在使用 Swift;恐怕我不知道swift,只知道objective-c。但这是在objective-c中如何完成的:
[self.navigationController pushViewController [[[CoolViewController alloc] initWithNibName: @"CoolDesign" bundle: nil] autorelease];
... 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".
...其中“CoolDesign”是您笔尖的基本名称。也就是说,您在 Interface Builder 中创建 CoolDesign.xib,Xcode 将 XML - text - xib 编译为 CoolDesign.nib,然后您告诉 initWithNibName 打开 @"CoolDesign"。
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.
仅仅告诉 Interface Builder 设计文档是一个UIViewController
. 虽然原则上 iOS 可以弄清楚你的意思,但原则上你可以为单个UIViewController
子类使用多个笔尖。
回答by Sudhin Davis
Try this, you can load a UIViewController
with nibName:
试试这个,你可以加载UIViewController
与nibName:
Swift
迅速
self.navigationController!.pushViewController(CoolViewController(nibName: "CoolViewControllerNibName", bundle: nil), animated: true )
Objective-C
目标-C
CoolViewController*coolViewCtrlObj=[[CoolViewController alloc] initWithNibName:@"CoolViewControllerNibName" bundle:nil];
[self.navigationController pushViewController:coolViewCtrlObj animated:YES];