UITabBarController 常用於在底部提供多個功能入口,讓使用者能在不同視圖控制器之間切換,而不必像導覽控制器那樣以堆疊方式推入或彈出。
通常可在應用程式代理的 applicationDidFinishLaunching 中建立 UITabBarController,建立多個子控制器後,放入陣列並指定給 viewControllers 屬性。
UITabBarController *tabBarController = [[UITabBarController alloc] init]; MyViewController *vc1 = [[MyViewController alloc] init]; MyOtherViewController *vc2 = [[MyOtherViewController alloc] init]; tabBarController.viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil]; [window addSubview:tabBarController.view];
可在各視圖控制器的 init 方法中建立 UITabBarItem,設定標題、圖片與標記,再指定給 tabBarItem 屬性。
實作 tabBarController:didSelectViewController: 可偵測使用者切換到哪個分頁,也能更新提示數字或儲存目前分頁狀態。