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

iOS 頁面跳轉全攻略:五種常見方式與適用場景

編輯:iOS 相關教學

說明:

以下一律以「FirstViewController(FVC)按下 button 後跳轉到 SecondViewController(SVC)」為例。

方式一:Storyboard 的 segues

按住 control 鍵把按鈕拖到 SVC 頁面,在彈出的 segue 面板中選擇跳轉模式即可。

優點:操作直觀、完全不用寫程式碼,跳轉關係在 Storyboard 上一目了然。

缺點:頁面一多,畫布就難以閱讀;團隊協作時衝突多、可維護性差,多人專案不建議採用。

方式二:UITabBarController 標籤頁控制器

呼叫 addChildViewController 加入子控制器:

 UITabBarController *tabbarVC = [[ UITabBarController alloc ] init ]; FirstViewController *FVC = [[FirstViewController ] init ]; FVC.tabBarItem.title = @"控制器1" ; FVC.tabBarItem.image = [ UIImage imageNamed : @"first.png" ]; SecondViewController *SVC = [[SecondViewController ] init ]; SVC.tabBarItem.title = @"控制器2" ; SVC. tabBarItem.image = [UIImage imageNamed : @"new.png" ]; // 加入子控制器(這些子控制器會自動進到 UITabBarController 的 viewControllers 陣列裡) [tabbarVC addChildViewController :FVC]; [tabbarVC addChildViewController :SVC];

優點:程式碼量極少。

缺點:系統原生 tabbar 外觀偏樸素,目前較少直接使用;若要用,建議自行客製。

方式三:UINavigationController 導航控制器

在 FVC 的按鈕事件方法中:

 [self.navigationController pushViewController:newC animated:YES]; //進入下一頁

在 SVC 中返回:

 [self.navigationController popViewControllerAnimated:YES]; //回到上一頁

若經歷多層跳轉後想一次回到根控制器:

 [ self .navigationController popToRootViewControllerAnimated: YES ]; //回到最初的頁面

方式四:Modal 形式彈出

在 FVC 中:

 [ self presentViewController:SVC animated: YES completion:nil];

在 SVC 中關閉:

 [ self dismissViewControllerAnimated: YES completion: nil ];

方式五:直接替換 UIWindow 的 rootViewController

總結

Storyboard 適合個人開發小專案,團隊協作或大型專案不建議。

UITabBarController 因原生樣式偏舊,目前不建議直接套用。

推薦 UINavigationController 與 Modal:兩者都沒有明顯短板,也是目前市面 App 的主流選擇,究竟選哪個取決於你的頁面是否需要導航列與返回堆疊。

以上就是 iOS 頁面跳轉的幾種常見做法整理,希望對你的開發有所幫助。

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