开发者

Why does a .xib file initialize despite me initWithNibFile: nil

I created a new window based applications. Immediately i created a class as a subclass of ViewController and in the appdelegate didFinishLaunchingWithOptions method i've added the following code

TestView myView = [[TestView alloc] initWithNibName:nil bundle:nil];

[self.window m开发者_C百科akeKeyAndVisible]; [self.window addSubview:mainMenu.view];

[mainMenu release]

However, despite this it still initializes the nib. If i placed a button in TestView.xib it shouldn't technically show it right? I initialized TestView without a nib? Why does it show up?


If you pass nil to -initWithNibName:bundle:, it will attempt to load a nib from the main bundle that has a filename that matches the name of the subclass, or the name of the subclass without the "Controller" suffix.


If you dont want it from a nib you should not init it in this way, it will still search for a xib that matches the class name exactly.

From the apple documentation found here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

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. (In this latter case, the class name becomes the name stored in the nibName property.) If you do none of these, the view controller will be unable to load its view.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜