views:

254

answers:

2

Hi,

When i create a iboutlet to my navigationController and try to add it to the currentview it isn't showing anything. Its really strange, when i initialize my navigationController in code and then add it to the view it works perfectly.

It even works when i make one in the mainWindows.xib and add it to the view in my applicationDidFinishLaunching method.

But as soon as i try to load it from another viewController it fails and i am left with a blank screen.

So it works when i add it programmatically or add it in my mainWindow.xib.

I have been trying for hours to get it working. It seems so simple, but i can't get it to work in other xib files.

My setup:

#import "RootViewController.h"

RootViewController *rootViewController=[[RootViewController alloc] init];
[window addSubview:rootViewController.navController.view];

My RootViewController has the ibOutlet to the navigationController.

Does anyone have a idea? I totally dont understand why this simple thing isn't working. What am i missing, its driving me crazy.

*UPDATE* The above code works if i change it to: I need to acces the rootViewController.view otherwise it won't work why?

#import "RootViewController.h"

RootViewController *rootViewController=[[RootViewController alloc] init];
if(rootViewController.view){
NSLog(@"nil"); 
}
[window addSubview:rootViewController.navController.view];

Thanks in advance!

A: 

All the times that this has happened to me the problem has always been that I forgot to connect something in Interface Builder. Make sure to check that your controller is connected to the rootViewController outlet in your app delegate or wherever. Also make sure window is connected.

If something doesn't get connected then you end up with nil in the outlet, so self.rootViewController.navController.view ends up being [[nil navController] view] which is nil and tries to add nil as a subview I think. You similarly end up talking to nil if window is nil, but it's probably rootViewController that isn't connected.

Nimrod
Thanks for your answer! i checked everything and it seemed oke, i solved it by accessing the rootViewController.view but it don't know why thats necessary
Bjorn
A: 

This is because accessing view actually invokes the getter, which creates the controller's view lazily (on-demand). The rootController's view doesn't exist until you first invoke the view getter method. The usual way to create the first app view is

[window addSubview:someController.view]

and it takes care of creating the view.

Adam Woś