简体   繁体   English

从“Nib文件”加载UIViewController

[英]Loading UIViewController “from” Nib File

I am trying to create an UIViewController from a Nib file. 我试图从Nib文件创建一个UIViewController On Google I found that I can only load an UIView from 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 - 不使用故事板

Try this, you can load a UIViewController with nibName: 试试这个,你可以加载一个带有nibName:UIViewController nibName:

Swift 迅速

self.navigationController!.pushViewController(CoolViewController(nibName: "CoolViewControllerNibName", bundle: nil), animated: true )

Objective-C Objective-C的

CoolViewController*coolViewCtrlObj=[[CoolViewController alloc] initWithNibName:@"CoolViewControllerNibName" bundle:nil];
[self.navigationController pushViewController:coolViewCtrlObj  animated:YES];

You need to allocate your ViewController , then initialize it by telling the iOS the name of the nib. 您需要分配ViewController ,然后通过告诉iOS nib的名称来初始化它。

I see you're using Swift; 我看到你正在使用Swift; I'm afraid I don't know swift, only objective-c. 我害怕我不知道快速,只有客观 - c。 But here is how it would be done in objective-c: 但是这里是如何在objective-c中完成的:

[self.navigationController pushViewController [[[CoolViewController alloc] initWithNibName: @"CoolDesign" bundle: nil] autorelease];

... where "CoolDesign" is the base name of your nib. ...“CoolDesign”是你的笔尖的基本名称。 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中创建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 . 仅告诉Interface Builder设计文档是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. 虽然原则上iOS可以弄清楚你的意思,但原则上你可以为一个UIViewController子类提供多个nib。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM