分欄控制器的概念:
? ? ?分欄控制器是管理多個視圖控制器的管理控制器,通過數(shù)組的方式管理多個平行關(guān)系的視圖控制器,與導(dǎo)航控制器的區(qū)別在于:導(dǎo)航控制器管理的是有層級關(guān)系的控制器。
? 分欄控制器在同一界面最多顯示5個控制器切換按鈕,超過5個時會自動創(chuàng)建一個新的導(dǎo)航控制器來管理其余的控制器。
UITabBarltem:分欄按鈕元素對象
badgeValue:分欄按鈕提示信息
selectedIndex:分欄控制器選中的控制器索引
viewControllers:分欄控制器管理數(shù)組
selectedViewController:分欄控制器選中的控制器對象
1.創(chuàng)建VCFirst,VCSecond,VCThrid視圖都繼承于UIViewController
【VCFirst.m】
@interface VCFirst()
@end
@implementation VCFirst
-(void)viewDidLoad{
[super viewDidLoad];
//方法一
//創(chuàng)建一個分欄按鈕,參數(shù)1:文字,參數(shù)2:顯示圖片,參數(shù)3:設(shè)置按鈕的tag值
/*UITabBarItem* tabBarItem=[[UITabBarItem alloc] initWithTitle:@"" image:nil tag:101];
self.tabBarItem=tabBarItem;*/
方法二
//根據(jù)系統(tǒng)風(fēng)格創(chuàng)建分欄按鈕,參數(shù)1:系統(tǒng)風(fēng)格設(shè)定
UITabBarItem* tabBarItem=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:101];
//按鈕右上角的提示信息,通常用來提示未讀的信息
tabBarItem.badgeValue=@"22";
self.tabBarItem=tabBarItem;
}
【AppDelegate.h】
#Import"AppDelegate.h"
#import"VCFirst.h"
#import"VCSecond.h"
#import"VCThird.h"
@interface AppDelegate()
@end
@implementation AppDelegate
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//創(chuàng)建Window
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//顯示window
[self.wind makeKeyAndVisible];
VCFirst* ?vcFirst=[[VCFirst allock]init];
vcFirst.view.backgroundcolor=[[UIColor blueColor];
//創(chuàng)建視圖控制器二
VCSecond* vcSecond=[[VCSecond alloc]init];
vcSecond.view.backgroundColor=[UIColor yellowColor];
VCThird* vcThird=[[VCThird alloc]init];
vcThird.view.backgroundColor=[UIColor orangeColor];
vcFirst.title=@"視圖1";
vcSecond.title=@"視圖2";
vcThird.title=@"視圖3";
UITabBarController* tbController=[[UITabBarController alloc]init];
//創(chuàng)建一個控制器數(shù)組對,將所有要被分欄制器管理的對象添加到數(shù)組中
NSArray* ?arrayVC=[NSArray arrayWithObjects:vcFirst,vcSecond,vcThrid.nil];
//將分欄視圖控制器管理數(shù)組賦值
tbController.viewControllers=arrayVC;
//將分欄控制器做為根視圖控制器
self.window.rootViewController=tbController;
//設(shè)置選中的視圖控器的索引,通過索引來確定顯示哪個一人控器,默認(rèn)值從0開始
tbController.selectedIndex=2;
if(tbController.selectedViewController==vcThird){
NSLog(@"當(dāng)前顯示的是控制器三~");
}
//設(shè)置分欄控制器的工具欄的透明度
tbController.tabBar.translucent=NO;
return YES;
}