iOS開(kāi)發(fā)之視圖切換
一、視圖切換
- UITabBarController (分頁(yè)控制器) - 平行管理視圖
- UINavigationController (導(dǎo)航控制器) - 壓棧出棧管理視圖
- 模態(tài)窗口
二、UITabBarController分頁(yè)控制器
- UITabBarController是為了利用 頁(yè)簽切換視圖 設(shè)計(jì)的控制器
- 該控制器有一個(gè)UITabBar控件,用戶通過(guò)點(diǎn)擊UITabBar進(jìn)行視圖切換
- UITabBarController本身會(huì)不顯示任何視圖,它只是一個(gè) 容器控制器
- 為了減少視圖間的耦合,所有UITabBarController的子視圖的相關(guān)標(biāo)題、圖標(biāo)等信息由子視圖自己控制
注意事項(xiàng):
- UITabBarController會(huì)一次性初始化所有子控制器,但默認(rèn)只加載第一個(gè)控制器視圖
- 每個(gè)視圖控制器都有一個(gè)tabBarController屬性,用它來(lái)訪問(wèn)所在的UITabBarController
- 每個(gè)視圖控制器都有一個(gè)tabBarItem屬性,用它來(lái)控制UITabBarController的UITabBar上的顯示信息
- tarBarItem的image屬性必須是png格式,并且打開(kāi)alpha通道 ,否則無(wú)法正常顯示
- UITabBarController通常是作為整個(gè)程序的rootViewController的,我們需要在程序的window顯示之前就創(chuàng)建好它。
具體步驟如下:
- 創(chuàng)建一個(gè)UITabBarController對(duì)象
- 創(chuàng)建UITabBarController中每一個(gè)tab對(duì)應(yīng)的要顯示的對(duì)象viewController
- 通過(guò)UITabBarController的viewControllers屬性將要顯示的所有viewController添加到UITabBarController中
- 通過(guò)設(shè)置UITabBarController對(duì)象為window.rootViewController,然后顯示window
//a.初始化一個(gè)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 = @"動(dòng)態(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為主窗口并顯示出來(lái)
[self.window makeKeyAndVisible];
UITabBarControllerDelegate代理
#pragma mark 該方法用于控制TabBarItem能不能選中
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
改變UITabBarController當(dāng)前顯示視圖的方法
- 改變selectedIndex屬性
- 改變selectedViewController屬性
- 改變viewControllers屬性
三、UINavigationController導(dǎo)航控制器
- UINavigationController中的子控制器以棧的形式存儲(chǔ),只有在棧頂部的控制器才能顯示在界面上
- 壓棧pushController,出棧popController
- UINavigationController必須有一個(gè)根控制器rootViewController
- 子控制器通過(guò)navigationController屬性訪問(wèn)UINavigationController
- 在棧中的子控制器都有一個(gè)導(dǎo)航欄navigationBar,通過(guò)navigationItem去控制
UINavigationItem屬于MVC中的Model,封裝了要顯示在UINavigationBar上的數(shù)據(jù):
title: 標(biāo)題
titleView :標(biāo)題視圖
leftBarButtonItem :左按鈕
rightBarButtonItem :右按鈕
下一個(gè)子視圖左側(cè)返回按鈕leftBarButtonItem的標(biāo)題優(yōu)先級(jí):
- 導(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)容無(wú)法操作
- (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;
- iOS實(shí)現(xiàn)日歷翻頁(yè)動(dòng)畫
- 實(shí)例講解iOS中的UIPageViewController翻頁(yè)視圖控制器
- 一步一步實(shí)現(xiàn)iOS主題皮膚切換效果
- Unity iOS混合開(kāi)發(fā)界面切換思路解析
- 比較IOS開(kāi)發(fā)中常用視圖的四種切換方式
- 淺析iOS多視圖滑動(dòng)點(diǎn)擊切換的集成
- iOS自定義鍵盤切換效果
- iOS應(yīng)用中使用Toolbar工具欄方式切換視圖的方法詳解
- iOS開(kāi)發(fā)中的ViewController轉(zhuǎn)場(chǎng)切換效果實(shí)現(xiàn)簡(jiǎn)介
- iOS圖片界面翻頁(yè)切換效果
相關(guān)文章
iOS?項(xiàng)目嵌入Flutter?運(yùn)行(最新推薦)
這篇文章主要介紹了iOS?項(xiàng)目嵌入Flutter?運(yùn)行,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03IOS中用正則表達(dá)式判斷輸入的內(nèi)容為8-16位且同時(shí)包含數(shù)字和字母
這篇文章主要介紹了IOS中用正則表達(dá)式判斷輸入的內(nèi)容為8-16位且同時(shí)包含數(shù)字和字母,需要的朋友可以參考下2017-06-06詳解iOS AFNetworking取消正在進(jìn)行的網(wǎng)絡(luò)請(qǐng)求
這篇文章主要介紹了詳解iOS AFNetworking取消正在進(jìn)行的網(wǎng)絡(luò)請(qǐng)求,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06解決JSON數(shù)據(jù)因?yàn)閚ull導(dǎo)致數(shù)據(jù)加載失敗的方法
前段時(shí)間發(fā)現(xiàn)一個(gè)問(wèn)題,當(dāng)JSON數(shù)據(jù)中有null會(huì)導(dǎo)致數(shù)據(jù)加載失敗,后來(lái)解決了,現(xiàn)在將解決方法分享給大家,有同樣問(wèn)題的朋友們可以參考。下面來(lái)一起看看吧。2016-09-09詳解 swift3.0 可選綁定共用同一塊內(nèi)存空間的實(shí)例
這篇文章主要介紹了詳解 swift3.0 可選綁定共用同一塊內(nèi)存空間的實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09iOS 使用UITextField自定義搜索框 實(shí)現(xiàn)用戶輸入完之后“實(shí)時(shí)搜索”功能
這篇文章主要介紹了iOS 使用UITextField自定義搜索框 實(shí)現(xiàn)用戶輸入完之后“實(shí)時(shí)搜索”功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03舉例講解設(shè)計(jì)模式中的原型模式在iOS應(yīng)用開(kāi)發(fā)中的作用
這篇文章主要介紹了設(shè)計(jì)模式中的原型模式在iOS應(yīng)用開(kāi)發(fā)中的作用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04