简体   繁体   English

为什么我要展示nil模态视图控制器?

[英]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 ." 我收到此错误“应用程序尝试在target上显示一个nil模态视图控制器。” 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 : 我使用的是,我认为您缺少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 . 您正在关闭applicationDidFinishLaunching的视图控制器。 But the AppDelegate is not a view controller, so there is nothing to dismiss. 但是AppDelegate不是视图控制器,因此没有什么可关闭的。

What exactly do you want to dismiss when starting the app? 在启动应用程序时,您到底要取消什么? I suppose you just want to present the correct VC, not dismiss it. 我想您只是想提供正确的VC,而不是关闭它。

Also, doing two animations in a row also normally does not work. 同样,连续执行两个动画通常也不起作用。 Consider doing the first with animated:NO instead. 考虑先用animated:NO代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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