陈斌彬的技术博客

Stay foolish,stay hungry

Storyboard 里面如何根据条件显示最初的 ViewController

请查看UIStoryboard这个类的文档,有接口实例化UIStoryboard中任何一个ViewController类,但是要在ViewController的属性中设置名称或者Storyboard ID才能用那个接口。

如果是在application:didFinishLaunchingWithOptions:方法中判断条件的话,就是把AppDelegate的window的rootViewController设置为你按条件实例化的那个实例。

最后,记得在返回YES之前调用[self.window makeKeyAndVisible];

给一段相对比较全面的参考代码:

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
if (userId) {
    MyViewController *firstController = [storyBoard instantiateViewControllerWithIdentifier:@"FirstLoadingController"];
    self.window.rootViewController = firstController;
} else {
    MyViewController *firstController = [storyBoard instantiateViewControllerWithIdentifier:@"IntroductionViewController"];
    self.window.rootViewController = firstController;
}
[self.window makeKeyAndVisible];