14

我已将 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 之间更改 File Owner 类。是的,我记得之后将它连接回视图插座。

请帮忙!我难住了。

4

1 回答 1

16

您是否覆盖了 PageViewController 中的 loadView 方法?如果你 NSLog 的 viewController1.view 怎么办?

实际上,在 Interface Builder 中,您必须将文件的所有者设置为 PageViewController,并将其视图连接到您在 Interface Builder 中的视图。

于 2009-07-01T10:29:51.377 回答