1.導航類型
UINavigationController 適用於父子頁面的跳轉
UITabBarController 適用於平級頁面的跳轉
2.presentViewController / dismissViewControllerAnimated和pushViewController / popViewController
(1)只有從UINavigationController導航過來的UIViewController 纔可以使用pushViewController / popViewController,從其它導航進入的只能使用presentViewController / dismissViewControllerAnimated
(2)如果A界面是通過presentViewController進入的,那麼在A界面裏只能使用presentViewController導航,如果A界面是通過UINavigationController包裝了一層進入,則可使用pushViewController導航。
(3)如果A界面是一個UITabBarController,進入時是通過presentViewController來導航的,那麼子Tab裏也只能通過presentViewController來導航,如果進入時是通過UINavigationController包裝了一層進入,則可使用[self.tabBarController.navigationController pushViewController:abcdViewController animated:true];這種方式導航。
3.修改UINavigationController導航進入後導航欄的圖標及文字
UIBarButtonItem* leftBar = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(homeBack)];
self.navigationItem.leftBarButtonItem = leftBar;
//可添加多個導航按鈕
// self.navigationItem.leftBarButtonItems = nil;
//同樣右邊的導航按鈕也可以設置
4.創建UITabBarController時需注意,要先創建出來,然後通過UINavigationController包裝後才能顯示,否則tab子項不顯示。創建格式如下
- TabOneViewController* oneVC = [[TabOneViewController alloc]init];
- oneVC.view.backgroundColor = [UIColor whiteColor];
- UITabBarItem* item1 = [[UITabBarItem alloc]initWithTitle:@"tab1" image:nil tag:0];
- oneVC.tabBarItem = item1;
- TabTwoViewController* twoVC = [[TabTwoViewController alloc]init];
- UITabBarItem* item2 = [[UITabBarItem alloc]initWithTitle:@"tab2" image:nil tag:1];
- twoVC.tabBarItem = item2;
- HomeViewController* homeVC = [[HomeViewController alloc]init];
- NSArray* array = [[NSArray alloc]initWithObjects:oneVC,twoVC, nil];
- homeVC.viewControllers = array;
- UINavigationController* navVC = [[UINavigationController alloc]initWithRootViewController:homeVC];
- [self presentViewController:navVC animated:YES completion:^{}];
- //下面這種創建了HomeViewController後,HomeViewController下的子Tab不能通過push或者pop導航
- // HomeViewController* homeVC = [[HomeViewController alloc]init];
- // [self presentViewController:homeVC animated:YES completion:^{}];
5.UITabBarController各頁面之間的切換
實現UITabBarControllerDelegate協議
//實現協議方法,用於切換Tab時,更改頁面的標題
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSInteger index = tabBarController.selectedIndex;
NSString *title;
switch (index) {
case 0:
title = @"Message";
break;
case 1:
title = @"User List";
break;
}
self.title