iOS中的多控制器管理(二)-UITabBarController-

來源:互聯網
上載者:User

標籤:uinavigationcontroller   應用   ios   控制器   

UITabBarControllerUITabBarController簡介
  • 跟UINavigationController類似,UITabBarController也可以輕鬆地管理多個控制器,輕鬆完成控制器之間的切換,典型例子就是QQ、等應用

UITabBarController的簡單使用UITabBarController的使用步驟
初始化UITabBarController設定UIWindow的rootViewController為UITabBarController根據具體情況,通過addChildViewController方法添加對應個數的子控制器
UITabBarController的子控制器UITabBarController添加控制器的方式有2種添加單個子控制器
- (void)addChildViewController:(UIViewController *)childController;
設定子控制器數組
@property(nonatomic,copy) NSArray *viewControllers;
UITabBarController的簡單建立
 // 1.建立視窗    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];// 2.建立tabBarController    UITabBarController *tabBarController = [[UITabBarController alloc] init]; // 3.設定視窗根控制器    self.window.rootViewController = tabBarController; // 3.1 建立三個子控制器    UIViewController *vc1 = [[UIViewController alloc] init];    vc1.view.backgroundColor = [UIColor redColor];    UIViewController *vc2 = [[UIViewController alloc] init];    vc2.view.backgroundColor = [UIColor greenColor];    UIViewController *vc3 = [[UIViewController alloc] init];    vc3.view.backgroundColor = [UIColor blueColor];    //[tabBarController addChildViewController:vc1];    //[tabBarController addChildViewController:vc2];    //[tabBarController addChildViewController:vc3];    tabBarController.viewControllers = @[vc1,vc2,vc3]; // 4.顯示    [self.window makeKeyAndVisible];
UITabBar
  • 如果UITabBarController有N個子控制器,那麼UITabBar內部就會有N個UITabBarButton作為子控制項

  • 如果UITabBarController有4個子控制器,那麼UITabBar的結構大致如所示

UITabBarButton

App主流架構結構

Segue簡介
  • Storyboard上每一根用來介面跳轉的線,都是一個UIStoryboardSegue對象(簡稱Segue)

Segue的屬性每一個Segue對象,都有3個屬性
唯一標識@property (nonatomic, readonly) NSString *identifier;來源控制器@property (nonatomic, readonly) id sourceViewController;目標控制器@property (nonatomic, readonly) id destinationViewController;

Segue的類型根據Segue的執行(跳轉)時刻,Segue可以分為2大類型
自動型:點擊某個控制項後(比如按鈕),自動執行Segue,自動完成介面跳轉手動型:需要通過寫代碼手動執行Segue,才能完成介面跳轉
自動型Segue
按住Control鍵,直接從控制項拖線到目標控制器點擊“登入”按鈕後,就會自動跳轉到右邊的控制器如果點擊某個控制項後,不需要做任何判斷,一定要跳轉到下一個介面,建議使用“自動型Segue”

手動型Segue
按住Control鍵,從來源控制器拖線到目標控制器手動型的Segue需要設定一個標識在恰當的時刻,使用perform方法執行對應的Segue
[self performSegueWithIdentifier:@"login2contacts" sender:nil];// Segue必須由來源控制器來執行,也就是說,這個perform方法必須由來源控制器來調用
如果點擊某個控制項後,需要做一些判斷,也就是說:滿足一定條件後才跳轉到下一個介面,建議使用“手動型Segue”

performSegueWithIdentifier 方法利用performSegueWithIdentifier:方法可以執行某個Segue,完成介面跳轉接下來研究performSegueWithIdentifier:sender:方法的完整執行過程
[self performSegueWithIdentifier:@“login2contacts” sender:nil];// 這個self是來源控制器
根據identifier去storyboard中找到對應的線,建立UIStoryboardSegue對象
設定Segue對象的sourceViewController(來源控制器)建立並且設定Segue對象的destinationViewController(目標控制器)
調用sourceViewController的下面方法,做一些跳轉前的準備工作並且傳入建立好的Segue對象
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;// 這個sender是當初performSegueWithIdentifier:sender:中傳入的sender
調用Segue對象的- (void)perform;方法開始執行介面跳轉操作如果segue的style是push
取得sourceViewController所在的UINavigationController調用UINavigationController的push方法將destinationViewController壓入棧中,完成跳轉
如果segue的style是modal
調用sourceViewController的presentViewController方法將destinationViewController展示出來
Sender參數的傳遞

控制器的資料傳遞控制器之間的資料傳遞主要有2種情況:順傳和逆傳順傳
控制器的跳轉方向: A 往 C資料的傳遞方向: A 往 C資料的傳遞方式: 在A的prepareForSegue:sender:方法中根據segue參數取得destinationViewController, 也就是控制器C, 直接給控制器C傳遞資料(要在C的viewDidLoad方法中取得資料,來賦值給介面上的UI控制項)

逆傳
控制器的跳轉方向: A 往 C資料的傳遞方向: C 往 A資料的傳遞方式: 讓A成為C的代理, 在C中調用A的代理方法,通過代理方法的參數傳遞資料給A

iOS中的多控制器管理(二)-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.