在 App 啟動時,常需要依登入狀態或引導流程決定初始頁面。使用 Storyboard 時,可以在 AppDelegate 的 didFinishLaunchingWithOptions 中建立 Storyboard,並取出對應的 ViewController 作為 rootViewController。
基本概念如下:
UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *next = [board instantiateViewControllerWithIdentifier:@"mainTabBar"]; self.window.rootViewController = next; [self.window makeKeyAndVisible];
若要依登入狀態分流,可以為不同頁面設定不同 Storyboard ID。例如已登入時載入首頁,未登入時載入登入頁。
NSString *identifier = isLogined ? @"mainTabBar" : @"loginViewController"; UIViewController *next = [board instantiateViewControllerWithIdentifier:identifier];
這種做法能讓啟動流程更清楚,也方便後續維護不同情境下的初始畫面。