日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

学习笔记:UITabBarController使用详解

發(fā)布時(shí)間:2025/7/25 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学习笔记:UITabBarController使用详解 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、手動(dòng)創(chuàng)建UITabBarController

  最常見(jiàn)的創(chuàng)建UITabBarController的地方就是在application delegate中的?applicationDidFinishLaunching:方法,因?yàn)閁ITabBarController通常是作為整個(gè)程序的rootViewController的,我們需要在程序的window顯示之前就創(chuàng)建好它,具體步驟如下:

  1、創(chuàng)建一個(gè)UITabBarController對(duì)象

  2、創(chuàng)建tabbarcontroller中每一個(gè)tab對(duì)應(yīng)的要顯示的對(duì)象

  3、通過(guò)UITabBarController的viewController屬性將要顯示的所有content viewcontroller添加到UITabBarController中

  4、通過(guò)設(shè)置UITabBarController對(duì)象為window.rootViewController,然后顯示window

  下面看一個(gè)簡(jiǎn)單的例子:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];// Override point for customization after application launch.
   SvTabBarFirstViewController *viewController1, *viewController2;
viewController1 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil];viewController1.title = @"First";viewController2 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil];viewController2.title = @"Second";self.tabBarController = [[[UITabBarController alloc] init] autorelease];self.tabBarController.delegate = self;self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
[viewController1 release];[viewController2 release];self.window.rootViewController = self.tabBarController;[self.window makeKeyAndVisible];return YES; }

?

二、UITabBarItem

  UITabBar上面顯示的每一個(gè)Tab都對(duì)應(yīng)著一個(gè)ViewController,我們可以通過(guò)設(shè)置viewcontroller.tabBarItem屬性來(lái)改變tabbar上對(duì)應(yīng)的tab顯示內(nèi)容。否則系統(tǒng)將會(huì)根據(jù)viewController的title自動(dòng)創(chuàng)建一個(gè),該tabBarItem只顯示文字,沒(méi)有圖像。當(dāng)我們自己創(chuàng)建UITabBarItem的時(shí)候,我們可以顯示的指定顯示的圖像和對(duì)應(yīng)的文字描述。當(dāng)然還可以通過(guò)setFinishedSelectedImage:withFinishedUnselectedImage:方法給選中狀態(tài)和飛選中狀態(tài)指定不同的圖片。下面看個(gè)自己創(chuàng)建UITabBarItem的小例子:

UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:2]; [item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]]; viewController2.tabBarItem = item; [item release];

  此外UITabBarItem還有一個(gè)屬性badgeValue,通過(guò)設(shè)置該屬性可以在其右上角顯示一個(gè)小的角標(biāo),通常用于提示用戶有新的消息,使用很簡(jiǎn)單,后面有例子。

?

三、moreNavigationController

  UITabBar上最多可以顯示5個(gè)Tab,當(dāng)我們往UITabBarController中添加超過(guò)的viewController超過(guò)5個(gè)時(shí)候,最后一個(gè)一個(gè)就會(huì)自動(dòng)變成,按照設(shè)置的viewControlles的順序,顯示前四個(gè)viewController的tabBarItem,后面的tabBarItem將不再顯示。當(dāng)點(diǎn)擊more時(shí)候?qū)?huì)彈出一個(gè)標(biāo)準(zhǔn)的navigationViewController,里面放有其它未顯示的的viewController,并且?guī)в幸粋€(gè)edit按鈕,通過(guò)點(diǎn)擊該按鈕可以進(jìn)入類(lèi)似與ipod程序中設(shè)置tabBar的編輯界面。編輯界面中默認(rèn)所有的viewController都是可以編輯的,我們可以通過(guò)設(shè)置UITabBarController的customizableViewControllers屬性來(lái)指定viewControllers的一個(gè)子集,即只允許一部分viewController是可以放到tabBar中顯示的。但是這塊兒要注意一個(gè)問(wèn)題就是每當(dāng)UITabBarController的viewControllers屬性發(fā)生變化的時(shí)候,customizableViewControllers就會(huì)自動(dòng)設(shè)置成跟viewControllers一致,即默認(rèn)的所有的viewController都是可以編輯的,如果我們要始終限制只是某一部分可編輯的話,記得在每次viewControlles發(fā)生改變的時(shí)候,重新設(shè)置一次customizableViewControllers。

  

四、UITabBarController的Rotation

  UITabBarController默認(rèn)只支持豎屏,當(dāng)設(shè)備方向放生變化時(shí)候,它會(huì)查詢(xún)viewControllers中包含的所有ViewController,僅當(dāng)所有的viewController都支持該方向時(shí),UITabBarController才會(huì)發(fā)生旋轉(zhuǎn),否則默認(rèn)的豎向。

  此處需要注意當(dāng)UITabBarController支持旋轉(zhuǎn),而且發(fā)生旋轉(zhuǎn)的時(shí)候,只有當(dāng)前顯示的viewController會(huì)接收到旋轉(zhuǎn)的消息。

?

五、UITabBar

?  UITabBar自己有一些方法是可以改變自身狀態(tài),但是對(duì)于UITabBarController自帶的tabBar,我們不能直接去修改其狀態(tài)。任何直接修改tabBar的操作將會(huì)拋出異常,下面看一個(gè)拋出異常的小例子:

self.tabBarController = [[[UITabBarController alloc] init] autorelease]; self.tabBarController.delegate = self; self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, nil];self.window.rootViewController = self.tabBarController; [self.window makeKeyAndVisible];self.tabBarController.tabBar.selectedItem = nil;

  上面代碼的最后一行直接修改了tabBar的狀態(tài),運(yùn)行程序回得到如下結(jié)果:

?

六、Change Selected Viewcontroller

  改變UITabBarController中當(dāng)前顯示的viewController,可以通過(guò)一下兩種方法:

  1、selectedIndex屬性

  通過(guò)該屬性可以獲得當(dāng)前選中的viewController,設(shè)置該屬性,可以顯示viewControllers中對(duì)應(yīng)的index的viewController。如果當(dāng)前選中的是MoreViewController的話,該屬性獲取出來(lái)的值是NSNotFound,而且通過(guò)該屬性也不能設(shè)置選中MoreViewController。設(shè)置index超出viewControllers的范圍,將會(huì)被忽略。

  2、selectedViewController屬性

  通過(guò)該屬性可以獲取到當(dāng)前顯示的viewController,通過(guò)設(shè)置該屬性可以設(shè)置當(dāng)前選中的viewController,同時(shí)更新selectedIndex。可以通過(guò)給該屬性賦值

tabBarController.moreNavigationController可以選中moreViewController。

  3、viewControllers屬性

  設(shè)置viewControllers屬性也會(huì)影響當(dāng)前選中的viewController,設(shè)置該屬性時(shí)UITabBarController首先會(huì)清空所有舊的viewController,然后部署新的viewController,接著嘗試重新選中上一次顯示的viewController,如果該viewController已經(jīng)不存在的話,會(huì)接著嘗試選中index和selectedIndex相同的viewController,如果該index無(wú)效的話,則默認(rèn)選中第一個(gè)viewController。

?

七、UITabBarControllerDelegate

  通過(guò)代理我們可以監(jiān)測(cè)UITabBarController的當(dāng)前選中viewController的變化,以及moreViewController中對(duì)編輯所有viewController的編輯。通過(guò)實(shí)現(xiàn)下面方法:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;

  該方法用于控制TabBarItem能不能選中,返回NO,將禁止用戶點(diǎn)擊某一個(gè)TabBarItem被選中。但是程序內(nèi)部還是可以通過(guò)直接setSelectedIndex選中該TabBarItem。

  下面這三個(gè)方法主要用于監(jiān)測(cè)對(duì)moreViewController中對(duì)view controller的edit操作

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers;- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;

  

七、附件UITabBarController測(cè)試程序源碼

?

SvTabBarAppDelegate.h這個(gè)頭文件,折疊后加進(jìn)去,總是無(wú)法展開(kāi),望大家見(jiàn)諒!

?

// // SvTabBarAppDelegate.h // SvTabBarControllerSample // // Created by maple on 5/19/12. // Copyright (c) 2012 smileEvday. All rights reserved. //#import <UIKit/UIKit.h>@interface SvTabBarAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) UITabBarController *tabBarController;@end

轉(zhuǎn)載于:https://www.cnblogs.com/lmg4819/p/4813768.html

總結(jié)

以上是生活随笔為你收集整理的学习笔记:UITabBarController使用详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。