为什么initWithNibName不适用于我的UIViewController子类? [英] Why doesn't initWithNibName work for my UIViewController subclass?
问题描述
我已经将UIViewController子类化为一个新类,PageViewController(我正在写一个简单的书应用程序)。我想添加一个从nib文件加载的新视图,并使用以下代码。它工作。
I have subclassed UIViewController into a new class, PageViewController (I'm writing a simple book app). I want to add a new view loaded from a nib file and am using the following code. It works.
PageViewController *viewController1 = [[UIViewController alloc] initWithNibName:@"Page1" bundle:nil];
[viewController1.view setUserInteractionEnabled:YES];
[self.view addSubview:viewController1.view];
但是,第一行是错误的,因为我应该在PageViewController上调用alloc。当我更正它(下面),代码编译,但xib文件不加载,视图只是透明。
However, the first line is wrong because I should be calling alloc on PageViewController. When I correct it (below), the code compiles but the xib file doesn't load and the view is just transparent.
PageViewController *viewController1 = [[PageViewController alloc] initWithNibName:@"Page1" bundle:nil];
[viewController1.view setUserInteractionEnabled:YES];
[self.view addSubview:viewController1.view];
PageViewController initWithNibName方法已取消注释,只是默认值,设置self = [super initWithNibName:nibNameOrNil
The PageViewController initWithNibName method has been uncommented and is just the default, setting self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil].
我试过的:
在Page1 nib文件中,我试图在PageViewController和UIViewController之间更改File Owner类。是的,我记得以后将其连接到视图出口。
What I have tried: In the Page1 nib file, I have tried changing the File Owner class between PageViewController and UIViewController. Yes, I remembered to connect it back to the view outlet afterwards.
帮助,请!
推荐答案
您是否覆盖了PageViewController中的loadView方法?
如果你NSLog的viewController1.view?
Have you overridden the loadView method in PageViewController? What if you NSLog the viewController1.view?
确实,在Interface Builder中,你必须设置文件的所有者为PageViewController,并将其视图您在Interface Builder中的视图。
Indeed, in Interface Builder, you have to set the file's owner to PageViewController, and connect its view to the view you have in Interface Builder.
这篇关于为什么initWithNibName不适用于我的UIViewController子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!