简体   繁体   中英

why am I getting presenting nil modal view controller?

I am getting this error "Application tried to present a nil modal view controller on target ." This is the code I have and I am trying to setup if condition is meet, it will change initial view controller.

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions
 {
 if(![[NSUserDefaults standardUserDefaults] boolForKey:@"logged_in"]) {


    ViewControllerOne *vc1 = [[ViewControllerOne alloc]init];
    vc1=[self.storyboard instantiateViewControllerWithIdentifier: @"vc1"];




    [self presentViewController:vc1 animated:YES completion:Nil];





} else {

    ViewControllerTwo *vc2 = [[ViewControllerTwo alloc]init];
    vc2=[self.storyboard instantiateViewControllerWithIdentifier: @"vc2"];




    [self presentViewController:vc2 animated:YES completion:Nil];

}
// Override point for customization after application launch.
return YES;
}

What I use is, I think you are missing the UIWindow :

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
    UIViewController *mainViewController = [storyboard instantiateInitialViewController];
    self.window.rootViewController = mainViewController;

    return YES;
}

Then you can replace:

[storyboard instantiateInitialViewController];

with:

[self.storyboard instantiateViewControllerWithIdentifier: @"vc1"];

You are dismissing a view controller in applicationDidFinishLaunching . But the AppDelegate is not a view controller, so there is nothing to dismiss.

What exactly do you want to dismiss when starting the app? I suppose you just want to present the correct VC, not dismiss it.

Also, doing two animations in a row also normally does not work. Consider doing the first with animated:NO instead.

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