开发者

how to associate view controller and xib file

开发者 https://www.devze.com 2023-04-10 15:41 出处:网络
When I create a uiviewcontroller from the \"new file...\" wizard with \"create xib file\" option, I can load it by just SomeViewController *view = [[SomeViewController alloc] init]. But when I rename

When I create a uiviewcontroller from the "new file..." wizard with "create xib file" option, I can load it by just SomeViewController *view = [[SomeViewController alloc] init]. But when I rename the xib file name, this code stops working, even though the file owner in the xib is still the right view controller.

I found that I can only get the view up by calling initWithNib. I am just wondering what was linking the xib with the view contro开发者_JAVA百科ller behind the scene? Can I still use init to get the xib loaded after renaming the file?

Regards

Leo


I can load it by just SomeViewController *view = [[SomeViewController alloc] init].

You can, but (IMO) you shouldn't. The designated initializer for UIViewController is -initWithNibName:bundle:. You might implement an -init method in your own view controller that calls [super initWithNibName:nil bundle:nil], but I think the code is clearer if you stick with the same name.

Also, make sure you read the documentation for UIViewController, particularly the discussion, which says:

If you specify nil for the nibName parameter, you must either override the loadView method and create your views there or you must provide a nib file in your bundle whose name (without the .nib extension) matches the name of your view controller class.

This is why the view controller will load a .xib file that has the same name as the view controller's class (or the name returned by the -nibName method, as explaind a little further on in the docs).

In short, UIViewController is functioning exactly as documented.

0

精彩评论

暂无评论...
验证码 换一张
取 消