如果有nib文件会调用loadView吗?

发布于 2024-12-06 19:16:02 字数 80 浏览 4 评论 0原文

如果我重写loadView方法,则无论是否有nib文件,都会调用loadView。如果我不重写loadView并且有一个nib文件,它会被调用吗?

If I override the loadView method, loadView will be called whether there is a nib file. If I don't override loadView and there is a nib file, will it be called ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

茶色山野 2024-12-13 19:16:02

是的,loadView 负责根据视图控制器的类名从已知的包中自动加载 nib 文件。如果您重写 loadView 并且不调用 [super loadView],则不会加载任何 nib。当 UIViewController 类的 view 属性被调用并且为 nil 时,它将调用 loadView

另请注意,覆盖 loadView 并调用 super 很可能不是您想要的。 loadView 用于设置 self.view 属性,仅此而已。其他一切都应该发生在 viewDidLoad 等中。

Yes, loadView is responsible for loading nib files automatically from known bundles based on the class name of the view controller. If you override loadView and don't call [super loadView], no nibs will be loaded. The UIViewController class will call loadView when its view property is called and is nil.

Also note that overriding loadView and calling super is most likely not what you want. loadView is for setting the self.view property, that's it. Everything else should happen in viewDidLoad etc.

π浅易 2024-12-13 19:16:02

是:

@implementation iPhoneHomeViewController

- (void)loadView {
    DEBUGLOG(@"view = %@, superview = %@", [self valueForKey:@"_view"], [[self valueForKey:@"_view"] superview]);
    [super loadView];
}

控制台:

GNU gdb 6.3.50-20050815 (Apple version gdb-1705) (Fri Jul  1 10:50:06 UTC 2011) 
Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU 
General Public License, and you are welcome to change it and/or distribute copies of it 
under certain conditions. Type "show copying" to see the conditions. There is absolutely 
no warranty for GDB.  Type "show warranty" for details. This GDB was configured as 
"x86_64-apple-darwin".sharedlibrary apply-load-rules all Attaching to process 1671. 

[Line: 40] -[iPhoneHomeViewController loadView]: view = (null), superview = (null)

Yes:

@implementation iPhoneHomeViewController

- (void)loadView {
    DEBUGLOG(@"view = %@, superview = %@", [self valueForKey:@"_view"], [[self valueForKey:@"_view"] superview]);
    [super loadView];
}

Console:

GNU gdb 6.3.50-20050815 (Apple version gdb-1705) (Fri Jul  1 10:50:06 UTC 2011) 
Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU 
General Public License, and you are welcome to change it and/or distribute copies of it 
under certain conditions. Type "show copying" to see the conditions. There is absolutely 
no warranty for GDB.  Type "show warranty" for details. This GDB was configured as 
"x86_64-apple-darwin".sharedlibrary apply-load-rules all Attaching to process 1671. 

[Line: 40] -[iPhoneHomeViewController loadView]: view = (null), superview = (null)
混吃等死 2024-12-13 19:16:02

是的。无论捆绑包是否有 Nib,加载视图都将始终被调用。
loadView 将负责为 UIViewController 加载视图,该视图可以来自任何来源。

因此,它以两种方式查找视图

1.检查属性nibFile,如果您调用了,则可以设置该属性initWithNibName:捆绑包:
或者如果您有情节提要 - 情节提要也存储在 nib 文件内创建的所有视图

2。如果您没有从任何一个源设置 nib,那么您打算自己创建视图。在这种情况下,您可以稍后将此视图设置为查看控制器的视图属性。

Yes. Load view will be always gets called irrespective of bundle has a Nib or not.
loadView will has the job of loading view for UIViewController which can come from any source.

So, it look for views in two ways

1. Check the property nibFile which can be set if you have called initWithNibName:bundle:
or if you have storyboard - storyboard stores all the views created inside a nib file too

2. If you don't set nib from either of the source then you are planning to create views yourself. In this case you can set this view to view controller's view property later.

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