由AppDelegate抛出的NSInternalInconsistencyException

时间:2016-10-25 12:33:58

标签: ios objective-c

我试图从我的AppDelegate打开一个故事板,但它引发了我的异常:

2016-10-25 10:26:16.776 momnt[22865:1300106] *** Terminating app due to  
 uncaught exception 'NSInternalInconsistencyException', 
 reason: 'Application windows are expected to have a root view 
 controller at the end of application launch'

以下是我要做的事情:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UIStoryboard *loginStoryboard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    UIViewController *mainViewController = [loginStoryboard instantiateInitialViewController];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = mainViewController;
    [self.window makeKeyAndVisible];

    return YES;
}

我已经读过新版本的XCode要求所有Windows都必须有rootViewController,但我已经做到了。

1 个答案:

答案 0 :(得分:1)

调试应用程序并检查mainViewController是否不是nil

问题似乎是你的storyBoard中没有任何viewController设置为初始viewcontroller。

设置您想要初始viewcontroller的任何视图控制器,并且您的代码可以正常工作。

<强> OR

使用instantiateViewControllerWithIdentifier并将您想要的viewController标识符作为初始viewController传递。

UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"Login" bundle:nil];

UIViewController *mainViewController = [storybord instantiateViewControllerWithIdentifier:@"InitialViewControllerID"];

self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];