开发者

iphone navigationController wont display unless i access .view

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:r开发者_Python百科ootViewController.navController.view];

Thanks in advance!


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.


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.


I have a similar problem actually even weirder, when the controller is connected only in IB the app loads fine on an iPad but on an iPhone I have a blank screen and the debugger shows that the view of the controller has a frame which is 0, 0, 0, 0 I fixed it by resetting the rootviewcontroller outlet in code in ApplicationDidFinishLaunching but I don't understand why this is not working

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜