placplacboom placplacboom - 1 month ago 20
Objective-C Question

NSInternalInconsistencyException throwing by AppDelegate

I'm trying to open a storyboard from my AppDelegate but it throws me the exception:

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'


Here is what i'm trying to do:

- (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;
}


I've read that newer versions of XCode requires that all the Windows must have a rootViewController but i've did that.

Answer

Debug the application and check if mainViewController is not nil.

Problem seems that you don't have any viewController set as initial viewcontroller in your storyBoard.

Set any viewcontroller you want to initial viewcontroller and your code work fine.

OR

Use instantiateViewControllerWithIdentifier and pass your viewController identifier to which you want as initial viewController.

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

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

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