您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

使用 Storyboard 切換啟動頁面

編輯:iOS 相關資訊

在 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];

這種做法能讓啟動流程更清楚,也方便後續維護不同情境下的初始畫面。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved