我已将 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 之间的文件所有者类。是的,我记得之后将其连接回视图插座。

请帮助!我很困惑。

有帮助吗?

解决方案

您是否重写了 PageViewController 中的 loadView 方法?如果你 NSLog viewController1.view 会怎么样?

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top