简体   繁体   中英

View Controller not presenting from App Delegate

I am trying to load a viewController if there are no accounts logged in however it gives me this error Warning: Attempt to present <DefaultViewController: 0xb96cac0> on <MainViewController: 0xb95c320> whose view is not in the window hierarchy! Here is the code that I am using right now:

BOOL returnAllOff = [[User data] returnAllOff];

if (returnAllOff) {
    DefaultViewController *viewController =[[DefaultViewController alloc]
    init];
    UIViewController *presentingController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    [presentingController presentViewController:viewController animated:YES completion:nil];

}

Actually this is the correct way of loading a ViewController from AppDelegate..

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

    DefaultViewController *viewController = [[DefaultViewController alloc] initWithNibName:@"DefaultViewController" bundle:nil];
    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];

Try this one by importing DefaultViewController in appDElegate.

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
DefaultViewController *viewController = [[DefaultViewController alloc] init];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM