开发者

iOS - Initializing UIViewControllers

In my app I have a UIViewController and a UIView named

  • HomeViewController
  • HomeView

They are both being loaded from a nib and the nib files are 开发者_运维知识库named the same as the class names When I try to initialize my ViewController:

HomeViewController *h = [[HomeViewController alloc] init];

It tries to load the viewController using the nib file for my UIView instead of the nib for my ViewController, If i load using initWithNibNamed it works fine.

Is this a bug in iOS I have never had this problem before. I am running against iOS 5 beta 5


It is the way that you are naming your view and viewcontroller which is causing the problem. From Apple's documentation (look at the nibName property)

  1. If the view controller class name ends with the word “Controller”, as in MyViewController, it looks for a nib file whose name matches the class name without the word “Controller”, as in MyView.nib.

  2. It looks for a nib file whose name matches the name of the view controller class. For example, if the class name is MyViewController, it looks for a MyViewController.nib file.

If you changed the HomeView to MyHomeView for example, your code will start working again. BUT using initWithNibName: method is the recommended way for initializing a ViewController

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜