为什么 initWithNibName 对我的 UIViewController 子类不起作用?
我已将 UIViewController 子类化为一个新类 PageViewController (我正在编写一个简单的图书应用程序)。 我想添加一个从 nib 文件加载的新视图,并使用以下代码。 有用。
PageViewController *viewController1 = [[UIViewController alloc] initWithNibName:@"Page1" bundle:nil];
[viewController1.view setUserInteractionEnabled:YES];
[self.view addSubview:viewController1.view];
但是,第一行是错误的,因为我应该在 PageViewController 上调用 alloc。 当我更正它时(如下),代码可以编译,但 xib 文件不会加载,并且视图只是透明的。
PageViewController *viewController1 = [[PageViewController alloc] initWithNibName:@"Page1" bundle:nil];
[viewController1.view setUserInteractionEnabled:YES];
[self.view addSubview:viewController1.view];
PageViewController initWithNibName 方法已取消注释,只是默认方法,设置 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]。
我尝试过的: 在 Page1 nib 文件中,我尝试更改 PageViewController 和 UIViewController 之间的文件所有者类。 是的,我记得之后将其连接回视图插座。
请帮助! 我很困惑。
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];
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];
The PageViewController initWithNibName method has been uncommented and is just the default, setting self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil].
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.
Help, please! I am stumped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否重写了 PageViewController 中的 loadView 方法?
如果你 NSLog viewController1.view 会怎么样?
事实上,在 Interface Builder 中,您必须将文件的所有者设置为 PageViewController,并将其视图连接到 Interface Builder 中的视图。
Have you overridden the loadView method in PageViewController?
What if you NSLog the viewController1.view?
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.