开发者社区> 问答> 正文

报错:nil modal view controller

得到了如下报错:**Application tried to present a nil modal view controller on target**。我在运行的程序是,首先判断条件是否满足,启动后修改初始化视图控制器。

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

展开
收起
爵霸 2016-03-26 09:59:35 2263 0
1 条回答
写回答
取消 提交回答
  • 如果是从storyboard中来创建控制器不需要先
    `
    ViewControllerOne *vc1 = [[ViewControllerOne alloc]init];
    `

    修改后的代码如下:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
         UIViewController *rootVC;
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
        if(![[NSUserDefaults standardUserDefaults] boolForKey:@"logged_in"]) {
               rootVC=[self.storyboard instantiateViewControllerWithIdentifier: @"vc1"];
        } else {
               rootVC=[self.storyboard instantiateViewControllerWithIdentifier: @"vc2"];
         }
        window.rootViewConroller=rootVC;
        [self.window makeKeyAndVisible];
        return YES;
    }
    2019-07-17 19:15:27
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
AutoAlerts-From Data to Action 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载