rootS在以下代码中为nil的任何原因

时间:2014-04-29 09:20:26

标签: ios

我正在做一个正常工作的登录视图,但我决定检查所有VC层次结构,我发现即使在我这样初始化之后,root也是nil:

       if([self isLogged] == TRUE) {
            identifier=@"ipadContainer";
        } else {
            identifier=@"iPadloginVC";

        }
        UIStoryboard *storyboardobj=[UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
        UIViewController *screen = [storyboardobj instantiateViewControllerWithIdentifier:identifier];
        UINavigationController *navCon = [[UINavigationController alloc]initWithRootViewController:screen];
        [self.window setRootViewController:navCon];
        [self.window makeKeyAndVisible];
        return YES;

因此,在加载应用程序后,我在iPadloginVC的viewDidLod中执行NSLog,

NSLog(@"%@",[[UIApplication sharedApplication] keyWindow].rootViewController);

NSLog指出我的rootVC为空,VC层次结构中有一个对象,它是iPadloginVC。

我错了还是NavController应该是这里的根而不是空指针?

1 个答案:

答案 0 :(得分:0)

让我们试试:

 UINavigationController *nvc = (UINavigationController *)self.window.rootViewController;
 // get view controller that you want