iOS UINavigationController與UITabBarController的組合使用

來源:互聯網
上載者:User

標籤:

 

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子項不顯示。建立格式如下

Java代碼  
  1. TabOneViewController* oneVC = [[TabOneViewController alloc]init];  
  2.     oneVC.view.backgroundColor = [UIColor whiteColor];  
  3.     UITabBarItem* item1 = [[UITabBarItem alloc]initWithTitle:@"tab1" image:nil tag:0];  
  4.     oneVC.tabBarItem = item1;  
  5.       
  6.     TabTwoViewController* twoVC = [[TabTwoViewController alloc]init];  
  7.     UITabBarItem* item2 = [[UITabBarItem alloc]initWithTitle:@"tab2" image:nil tag:1];  
  8.     twoVC.tabBarItem = item2;  
  9.   
  10.     HomeViewController* homeVC = [[HomeViewController alloc]init];  
  11.     NSArray* array = [[NSArray alloc]initWithObjects:oneVC,twoVC, nil];  
  12.     homeVC.viewControllers = array;  
  13.       
  14.     UINavigationController* navVC = [[UINavigationController alloc]initWithRootViewController:homeVC];  
  15.     [self presentViewController:navVC animated:YES completion:^{}];  
  16.       
  17.     //下面這種建立了HomeViewController後,HomeViewController下的子Tab不能通過push或者pop導航  
  18.     //    HomeViewController* homeVC = [[HomeViewController alloc]init];  
  19.     //    [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 

 

iOS UINavigationController與UITabBarController的組合使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.