LoginSignup
116
116

More than 5 years have passed since last update.

起動時に表示する ViewController を動的に変更する

Posted at

ViewController に Storyboard ID を設定する

storyboard 中の表示したい ViewController に Storyboard ID を設定する。

AppDelegate 内で読み出す

初回起動時や特定の条件によって表示する ViewController を指定します。下の例は初回起動時に表示画面を切り替える例。

AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...

    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    bool isFirst = NO;
    // 初回起動かどうかを判定する処理
    ...

    // ここで表示したい ViewController を指定する
    UIViewController *viewController;
    if(isFirst){
        viewController = [storyboard instantiateViewControllerWithIdentifier@"initial view controller storyboard id"];
    }else{
        viewController = [storyboard instantiateViewControllerWithIdentifier@"main view controller storyboard id"];
    }

    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

おまけ

別の Storyboard にある ViewController に遷移する

1つの Storyboard で全画面遷移を記述すると大変なので複数の Storyboard に分けて開発することも可能です。

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard2" bundle:nil];
    id firstViewController = [storyboard instantiateInitialViewController];
    [self presentViewController:(UIViewController *)firstViewController animated:NO completion:nil];
116
116
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
116
116