aryaxt
aryaxt

Reputation: 77616

iOS - Initializing UIViewControllers

In my app I have a UIViewController and a UIView named

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

Upvotes: 1

Views: 533

Answers (1)

Numan Tariq
Numan Tariq

Reputation: 1296

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

Upvotes: 2

Related Questions