UITabBarController 基本用法
?
?
?
一、UITabBarController對象的創建和初始化
?
首先創建多個視圖控制器,并在tabBarItem中添加標題和圖片
?
???self.viewController.tabBarItem.title?=?@"首頁";//設置tabBarItem標題
????self.viewController.tabBarItem.image?= [UIImage?imageNamed:@"icon_home"];//添加圖片
????self.viewController.tabBarItem.badgeValue?=?@"1";//消息
????FirstViewController?*one = [[FirstViewController?alloc]?init];
????one.tabBarItem.title?=?@"消息";
????one.tabBarItem.image?= [UIImage?imageNamed:@"icon_meassage"];
????SecondViewController?*two = [[SecondViewController?alloc]?init];
????two.tabBarItem.title?=?@"聯系人";
????two.tabBarItem.image?= [UIImage?imageNamed:@"icon_selfinfo"];
????ThirdViewController?*three = [[ThirdViewController?alloc]?init];
???three.title?=?@"搜索";
????three.tabBarItem.image?= [UIImage?imageNamed:@"icon_search"];
????ForthViewController?*four = [[ForthViewController?alloc]?init];
????four.tabBarItem.title?=?@"更多";
????four.tabBarItem.image?= [UIImage?imageNamed:@"icon_more"];
????FifthViewController?*five = [[FifthViewController?alloc]init];
????five.tabBarItem.title?=?@"訂閱";
????SixthViewController?*six = [[SixthViewController?alloc]?init];
????six.tabBarItem.title?=?@"下載";
????
????UINavigationController?*nav = [[UINavigationController?alloc]initWithRootViewController:three];
?
??注意,第三個視圖控制器為導航控制器?
?
將多個視圖控制器添加到數組(視圖超過5個以后,自動生成More,隱藏多出來的視圖控制器)
效果為多出來下邊一排視圖切換工具
????NSArray?*vcArray = [NSArray?arrayWithObjects:self.viewController, one, two, nav, four, five, six,?nil];//創建視圖控制器數組?
????UITabBarController?*tabBarC = [[UITabBarController?alloc]?init];
????tabBarC.tabBar.selectedImageTintColor?= [UIColor?redColor];//選中時圖片顏色變為紅色
????tabBarC.viewControllers?= vcArray;//將多個視圖控制器組織到tab控制器中,以選項卡
?
???self.window.rootViewController?= tabBarC;//設置根視圖控制器為tabBarController
?
二、UITabBarController可以實現UITabBarControllerDelegate協議
?
tabBarC.delegate?=?self; //設置self為代理
可以重寫代理中方法(很少用),表示選中了某個ViewController
?
-(void)tabBarController:(UITabBarController?*)tabBarController didSelectViewController:(UIViewController*)viewController{
//執行相應操作
}
?
?
//代碼解說
- (BOOL)application:(UIApplication?*)application didFinishLaunchingWithOptions:(NSDictionary?*)launchOptions
{
????self.window?= [[[UIWindow?alloc]?initWithFrame:[[UIScreen?mainScreen]?bounds]]?autorelease];
????FirstViewController?*firstController=[[FirstViewController?alloc]?init];
????//設置標題
????firstController.title=@"First";
????//設置tabBarItem的樣式(這種方式是按照系統定義的方式)
????firstController.tabBarItem=[[UITabBarItem?alloc]?initWithTabBarSystemItem:UITabBarSystemItemMoretag:101];
????//也可以用自定義方式方式如下:
????
????
????SecondViewController?*secondController=[[SecondViewController?alloc]?init];
????secondController.title=@"Second";
????secondController.tabBarItem=[[UITabBarItem?alloc]?initWithTabBarSystemItem:UITabBarSystemItemHistorytag:102];
????ThirdViewController?*thirdController=[[ThirdViewController?alloc]?init];
????thirdController.title=@"Third";
????thirdController.tabBarItem=[[UITabBarItem?alloc]?initWithTabBarSystemItem:UITabBarSystemItemFavoritestag:103];
????
????UINavigationController?*nav1=[[UINavigationController?alloc]?initWithRootViewController:firstController];
????[firstController?release];
????UINavigationController?*nav2=[[UINavigationController?alloc]?initWithRootViewController:secondController];
????[secondController?release];
????UINavigationController?*nav3=[[UINavigationController?alloc]?initWithRootViewController:thirdController];
????[thirdController?release];
????NSArray?*controllersArray=[[NSArrayalloc]?initWithObjects:nav1,nav2,nav3,?nil];
????[nav1?release];
????[nav2?release];
????[nav3?release];
????
????UITabBarController?*tabController=[[UITabBarController?alloc]?init];
????tabController.viewControllers=controllersArray;
????tabController.selectedIndex=0;
????tabController.delegate=self;//在AppDelegate.h文件內實現UITabBarControllerDelegate協議
????self.tabBarController=tabController;
????[tabController?release];
????[controllersArray?release];
????
????[self.window?addSubview:tabController.view];
?
????[self.window?makeKeyAndVisible];
????returnYES;
}
?//當點擊tabBarItem時觸發該操作??UITabBarControllerDelegate中的一個方法
- (void)tabBarController:(UITabBarController?*)tabBarController didSelectViewController:(UIViewController?*)viewController?{
????NSLog(@"%d",?viewController.tabBarItem.tag);
}?
轉載于:https://www.cnblogs.com/zfrankice/p/4238998.html
總結
以上是生活随笔為你收集整理的UITabBarController 基本用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 鸟哥私房菜(基础篇第三版)笔记
- 下一篇: 程序员如何优雅地使用 Mac?