iOS開發(fā)之視圖切換
一、視圖切換
- UITabBarController (分頁控制器) - 平行管理視圖
- UINavigationController (導(dǎo)航控制器) - 壓棧出棧管理視圖
- 模態(tài)窗口
二、UITabBarController分頁控制器
- UITabBarController是為了利用 頁簽切換視圖 設(shè)計的控制器
- 該控制器有一個UITabBar控件,用戶通過點擊UITabBar進行視圖切換
- UITabBarController本身會不顯示任何視圖,它只是一個 容器控制器
- 為了減少視圖間的耦合,所有UITabBarController的子視圖的相關(guān)標(biāo)題、圖標(biāo)等信息由子視圖自己控制

注意事項:
- UITabBarController會一次性初始化所有子控制器,但默認(rèn)只加載第一個控制器視圖
- 每個視圖控制器都有一個tabBarController屬性,用它來訪問所在的UITabBarController
- 每個視圖控制器都有一個tabBarItem屬性,用它來控制UITabBarController的UITabBar上的顯示信息
- tarBarItem的image屬性必須是png格式,并且打開alpha通道 ,否則無法正常顯示
- UITabBarController通常是作為整個程序的rootViewController的,我們需要在程序的window顯示之前就創(chuàng)建好它。
具體步驟如下:
- 創(chuàng)建一個UITabBarController對象
- 創(chuàng)建UITabBarController中每一個tab對應(yīng)的要顯示的對象viewController
- 通過UITabBarController的viewControllers屬性將要顯示的所有viewController添加到UITabBarController中
- 通過設(shè)置UITabBarController對象為window.rootViewController,然后顯示window
//a.初始化一個tabBar控制器
UITabBarController *tarbarVC = [[UITabBarController alloc] init];
//設(shè)置控制器為Window的根控制器
self.window.rootViewController = tarbarVC;
//b.創(chuàng)建子控制器
UIViewController *c1 = [[UIViewController alloc] init];
c1.view.backgroundColor = [UIColor grayColor];
c1.view.backgroundColor=[UIColor greenColor];
c1.tabBarItem.title = @"消息";
c1.tabBarItem.image = [UIImage imageNamed:@"tab_recent_nor"];
c1.tabBarItem.badgeValue = @"123";
UIViewController *c2 = [[UIViewController alloc] init];
c2.view.backgroundColor = [UIColor brownColor];
c2.tabBarItem.title = @"聯(lián)系人";
c2.tabBarItem.image = [UIImage imageNamed:@"tab_buddy_nor"];
UIViewController *c3 = [[UIViewController alloc] init];
c3.tabBarItem.title = @"動態(tài)";
c3.tabBarItem.image = [UIImage imageNamed:@"tab_qworld_nor"];
UIViewController *c4 = [[UIViewController alloc] init];
c4.tabBarItem.title = @"設(shè)置";
c4.tabBarItem.image = [UIImage imageNamed:@"tab_me_nor"];
//c.添加子控制器到ITabBarController中
tarbarVC.viewControllers = @[c1,c2,c3,c4];
//d.設(shè)置Window為主窗口并顯示出來
[self.window makeKeyAndVisible];
UITabBarControllerDelegate代理
#pragma mark 該方法用于控制TabBarItem能不能選中
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
改變UITabBarController當(dāng)前顯示視圖的方法
- 改變selectedIndex屬性
- 改變selectedViewController屬性
- 改變viewControllers屬性
三、UINavigationController導(dǎo)航控制器
- UINavigationController中的子控制器以棧的形式存儲,只有在棧頂部的控制器才能顯示在界面上
- 壓棧pushController,出棧popController
- UINavigationController必須有一個根控制器rootViewController
- 子控制器通過navigationController屬性訪問UINavigationController
- 在棧中的子控制器都有一個導(dǎo)航欄navigationBar,通過navigationItem去控制

UINavigationItem屬于MVC中的Model,封裝了要顯示在UINavigationBar上的數(shù)據(jù):
title: 標(biāo)題
titleView :標(biāo)題視圖
leftBarButtonItem :左按鈕
rightBarButtonItem :右按鈕
下一個子視圖左側(cè)返回按鈕leftBarButtonItem的標(biāo)題優(yōu)先級:
- 導(dǎo)航欄返回按鈕backBarButtonItem的標(biāo)題
- 導(dǎo)航欄navigationItem的標(biāo)題
- 視圖控制器的標(biāo)題
UINavigationController常用的主要方法:
#pragma mark 壓棧,把控制器壓入導(dǎo)航控制器子控制器棧中
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
#pragma mark 出棧,把導(dǎo)航控制器子控制器棧的棧頂彈出
- (void)popViewControllerAnimated:(BOOL)animated;
#pragma mark 多次出棧直到棧頂為指定控制器
- (void)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
#pragma mark 多次出棧直到棧頂為根控制器
- (void)popToRootViewControllerAnimated:(BOOL)animated;
四、模態(tài)窗口
#pragma mark 從下方彈出指定的視圖控制器,賦予模態(tài),即當(dāng)前視圖關(guān)閉前,其他視圖上的內(nèi)容無法操作
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion;
#pragma mark 關(guān)閉模態(tài)窗口,該方法在模態(tài)窗口中調(diào)用
- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion;
相關(guān)文章
iOS中UIAlertView3秒后消失的兩種實現(xiàn)方法
這篇文章主要介紹了iOS中UIAlertView3秒后消失的兩種實現(xiàn)方法,實現(xiàn)方法涉及到NSTimer和PerformSelector:withObject:afterDelay:方法的結(jié)合使用,需要的朋友可以參考下2017-12-12
iOS10語音識別框架SpeechFramework應(yīng)用詳解
在iOS10系統(tǒng)了,apple開放了與語音識別相關(guān)的接口,開發(fā)者可以將其應(yīng)用到自己的App中,實現(xiàn)用戶通過語音進行功能操作。 這篇文章主要介紹了iOS10語音識別框架SpeechFramework應(yīng)用,需要的朋友可以參考下2016-09-09
詳解iOS - ASIHTTPRequest 網(wǎng)絡(luò)請求
本篇文章主要介紹了iOS - ASIHTTPRequest 網(wǎng)絡(luò)請求 ,詳細(xì)的介紹了 ASIHTTPRequest的使用,具有一定的參考價值,有興趣的可以了解一下。2016-12-12
iOS開發(fā)技能weak和strong修飾符的規(guī)范使用詳解
這篇文章主要為大家介紹了iOS開發(fā)技能weak和strong修飾符的規(guī)范使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07
IOS 開發(fā)之讀取addressbook的實現(xiàn)實例
這篇文章主要介紹了IOS 開發(fā)之讀取addressbook的實現(xiàn)實例的相關(guān)資料,希望通過本文大家能夠掌握這樣的內(nèi)容,需要的朋友可以參考下2017-09-09

