maggix
maggix

Reputation: 3278

I can make UINavigationController load only at 2nd level, not at Root View Controller

I tried looking for a similar problem but I could not find a similar question.

I am loading a UINavigationController in a UIView which is not (as in most examples) the MainWindow.

I created one new .xib called DocumentsViewController which is subclass of UIView (it has the related .m and .h files). And I created a DocumentsRootViewController.xib, which is a subclass of UITableViewController, which is supposed to be the UINavigationController's RootViewController.

I moved to DocumentsViewController and added a UINavigationController object in Interface Builder. Then I went to code, and added it as in IBOutlet, and connected it to the object.

In the ViewDidLoad, I execute the following lines:

DocumentsRootViewController *rootViewController = [[[DocumentsRootViewController alloc] init] autorelease];
rootViewController.title = @"Documents";
[navigationControllerDocuments initWithRootViewController:rootViewController];
[self.view addSubview:navigationControllerDocuments.view];    

It shows the table as intended, but it shows a "Back" button to the "Root View Controller" (as in picture below).

Why? Shouldn't it already know that the rootviewcontroller has been set?

Thank you in advance to the ones that clarify this doubt

Giovanni

The UINavigationController behavior Xib Structure

Upvotes: 0

Views: 651

Answers (1)

gamozzii
gamozzii

Reputation: 3921

When you add the UINavigationController via the Nib it actually creates an instance of a UINavigationController inside the nib file with a default RootViewController set (of type UIViewController) and with a default title of RootViewController.

When you load the nib, this object is being created as part of loading the nib (i.e when you initialise DocumentsViewController) - so the navigationControllerDocuments outlet is already initialised as a UINavigationController with the default ViewController already set on it.

What I think is happening is when you call 'initWithRootViewController' - you are calling this on an already initialised object - so it is running the initialisation code again - pushing the second view controller (the DocumentRootViewController) onto the stack, but the default one that was created in the nib is already there.

What you should probably do is forget about creating one in the nib and initialise the whole thing programatically.

i.e. where you do:

[navigationControllerDocuments initWithRootViewController:rootViewController];

I suggest that you do an alloc and init instead:

[[navigationControllerDocuments alloc] initWithRootViewController:rootViewController];

Since you are doing this you really don't need to have the navigation controller added to the nib so if this works you should remove it from the nib since you are replacing it with this one in code.

Upvotes: 1

Related Questions