Friday, October 02, 2009

Objective C Hello World

This is frustrating. I'm carefully following the iPhone HelloWorld tutorial, but I still have run into a problem with the Adding a View Controller step. At the end of the section, I test the application in the simulator and it promptly crashes. Here's the error message in the console (Run > Console):

2009-10-02 14:01:04.641 HelloWorld[5358:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] was unable to load a nib named "MyViewController"'


This appears to be a reference to the following code in HelloWorldAppDelegate.m:

MyViewController *aViewController = [[MyViewController alloc]

initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];


The implication is that the nib file MyViewController.xib can't be found. That file is in fact in my project; it's in the Classes folder, where is was automatically created. This must be user error, because I can't find a single other person via Google that's having the same problem. I tried using the source provided in the tutorial, but the application still crashes. Maybe it's something wrong with my environment?

I poked around, and thanks to a tip from a blog and the official documentation, I was able to get the application to work by using nil instead of specifying the nib file name:

MyViewController *aViewController = [[MyViewController alloc]

initWithNibName:nil bundle:[NSBundle mainBundle]];


Supposedly, specifying nil instead of the nib file name tells the view controller class to assume the nib file has the same name as the class, which in this case is "MyViewController". No idea why this should when hardcoding the name doesn't.

But it turns out this didn't fix the problem. I continued to the next section of the tutorial and changed the MyViewController view background color to lavendar, but the background still shows up white when I run the app. On a hunch, I changed the background of the MainWindow view to green. Now the background shows up green. So no view is getting loaded.



No comments: