2 bluetooth99 bluetooth99 于 2013.01.08 09:54 提问

报错: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;
}

2个回答

zhanglei5415
zhanglei5415   Rxr 2013.01.08 11:56
已采纳

如果是从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;
}
xinyoulingxi1_tong
xinyoulingxi1_tong   2013.01.08 10:53

我分析你是少了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;
}

然后去掉这句:

[storyboard instantiateInitialViewController];

加上这句

[self.storyboard instantiateViewControllerWithIdentifier: @"vc1"];
qq_31683103
qq_31683103 我能问一下,就是我已经把storyboard已经删除了,但是在iOS7.0的系统上就是抱着,然后没有storyboard这个东西,我怎么设置 instantiateInitialViewController 这个初始化的视图控制器呢,再谁的基础上设置呢?
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!