您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 專案常用模板:UITabBar 與 UINavigationController 組合

編輯:iOS 開發綜合

模板概念

在實際專案中,UITabBarController 搭配 UINavigationController 是非常常見的結構。TabBar 負責主要功能切換,NavigationController 則負責每一頁內部的堆疊導覽。

建立基本結構

通常會在 AppDelegate 中建立 window、tabBarController,再把各個功能頁包進 NavigationController,最後加入 tabBarController。

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

FirstViewController *one = [[FirstViewController alloc] init];
SecondViewController *second = [[SecondViewController alloc] init];

UINavigationController *navFirst = [[UINavigationController alloc] initWithRootViewController:one];
UINavigationController *navSecond = [[UINavigationController alloc] initWithRootViewController:second];

[self.tabBarController addChildViewController:navFirst];
[self.tabBarController addChildViewController:navSecond];

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

設定 TabBarItem

每個主要頁面都可以設定標題與圖示,讓使用者在底部標籤欄辨識目前所在頁面。

one.title = @"聯絡人";
one.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"one" image:[UIImage imageNamed:@""] selectedImage:nil];

二級頁面跳轉

在主要頁面中,可以透過 navigationController push 二級頁面,形成堆疊式導覽。

BackViewController *back = [[BackViewController alloc] init];
back.title = @"第二視圖層";
[self.navigationController pushViewController:back animated:NO];

小結

這個模板幾乎涵蓋多數 App 的基本導覽需求。只要掌握 rootViewController 設定、TabBarItem 配置與 push 流程,就能快速建立可擴充的專案骨架。

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