为什么我要呈现nil模态视图控制器?

时间:2013-01-07 15:03:45

标签: iphone objective-c ios xcode

我收到此错误“应用程序尝试在目标上显示nil模态视图控制器。”  这是我的代码,如果条件满足,我试图设置它,它将改变初始视图控制器。

   - (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 个答案:

答案 0 :(得分:0)

我使用的是,我认为你错过了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"];

答案 1 :(得分:0)

您正在解雇applicationDidFinishLaunching中的视图控制器。但是AppDelegate不是一个视图控制器,所以没有什么可以解雇的。

启动应用时,您想要解雇的是什么?我想你只想呈现正确的VC,而不是解雇它。

此外,连续两个动画也通常不起作用。考虑使用animated:NO代替第一个。