iOS - 如果应用程序首次启动 - 呈现ModalView

时间:2014-08-21 16:50:03

标签: ios storyboard

我想出了如何在应用程序启动时呈现两个不同的视图控制器,如果我不以模态方式呈现它们,它可以正常工作。如果我改变代码,它会以某种方式显示空白视图控制器 来自 - > self.window.rootViewController = landingVC; TO-> [self.window.rootViewController presentViewController:landingVC animated:YES completion:nil];

这是我的app delegate中的完整代码:

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

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *tabbarController = [storyboard instantiateViewControllerWithIdentifier:@"TabBarViewController"];
UIViewController *landingVC = [storyboard instantiateViewControllerWithIdentifier:@"LandingViewController"];

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) {

        self.window.rootViewController = tabbarController;
 } else {
  [self.window.rootViewController presentViewController:landingVC animated:YES completion:nil];
      }

[self.window makeKeyAndVisible];
return YES;
}

AFAIK,这就是应该如何显示modalVC。我已经验证了storyboardID并检查过"使用storyboardIDs"对于两个视图控制器。

1 个答案:

答案 0 :(得分:0)

你正在做的是将根视图控制器设置为tabbarController,如果应用程序之前已经启动过,否则你告诉应用程序的根视图控制器(尚未设置但尚未存在)呈现视图控制器。

我愿意猜测,如果你把NSLog(@“%@”,self.window.rootViewController);在presentViewController行之上,你会得到nil,这意味着你试图告诉nil做某事,这当然是行不通的。

我刚试过这个并且有效:

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

    UIViewController *testRootVC = [UIViewController new];
    testRootVC.view.backgroundColor = [UIColor blueColor];
    self.window.rootViewController = testRootVC;

    [self.window makeKeyAndVisible];

    if (YES) {
        UIViewController *testModalVC = [UIViewController new];
        testModalVC.view.backgroundColor = [UIColor redColor];
        [testRootVC presentViewController:testModalVC animated:YES completion:nil];
    }

    return YES;
}