为什么 initWithNibName 对我的 UIViewController 子类不起作用?

发布于 2024-07-25 17:09:50 字数 940 浏览 3 评论 0原文

我已将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如歌彻婉言 2024-08-01 17:09:50

您是否重写了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文