initWithNibName has no effect


#1

The initialization of the CurrentTimeViewController.xib file seems to have no effect.

When I write the code like this:

	[super initWithNibName:nil bundle:nil];

it seems to have the same effect as doing it this way:

	[super initWithNibName:@"CurrentTimeViewController" bundle:nil];

Both versions work. The view is loaded successfully. I think this is because the connection to the ViewController is already set in the nib-file by File’s Owner.
Is this correct? If “Yes” - why do we need to set this relationship again programmatically in the init-method?

I am sorry that I can’t tell the correct page # in the book because I am using the Kindle version.


#2

If you give a nil as the name of the nib file, the UIViewController will go looking for a nib file with the same name as the UIViewController’s class. Thus, if you name your UIViewController subclass Foo, the default nib file is Foo.nib.


#3

hm, I tried to change the name of the nib file to something completely different than the name of the UIViewController subclass and the nib file still gets loaded.
I even tried to remove the

[super initWithNibName:nil bundle:nil];

statement and also removed the build files and did a 'Clean All Targets".
The nib file still gets loaded correctly. It seems logical to me to use this statement, but it looks like it has no effect in the context it is being used.


#4

Are you sure the correct initialization method is being called and you have overridden the designated initializer of UIViewController? It might be hopping the step where you call the superclass’ initializer in your own initializer.

If I try and load a NIB named @“FOOBAR” (which doesn’t exist), the application crashes and I get the following:

*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Could not load NIB in bundle: ‘NSBundle </Users/joeconway/Library/Application Support/iPhone Simulator/4.0/Applications/76A28D8F-B748-4460-B55B-1C77D55FD25F/Tester.app> (loaded)’ with name ‘FOOBAR’’


#5

This is what I did:

To be completely sure that the misunderstanding comes NOT from my own bogus code I used the HypnoTime.xcodeproj provided with the download companion files.
I renamed CurrentTimeViewController.xib into TimeViewController.xib went to the implementation of CurrentTimeViewController (CurrentTimeViewController.m) and changed the line [super initWithNibName:@"CurrentTimeViewController" bundle:nil]; to [super initWithNibName:nil bundle:nil]; built and run the code and it worked well. Even if I delete the whole line: [super initWithNibName:nil bundle:nil]; the code gets executed. I still don’t see where the relationship between the nib file and the ViewController subclass is built :confused: