iOS基础之UITabBarController(标签视图控制器)
UITabBarController是可以帮我们添加、管理许多的标签项,使我们的程序包含不同的操作模式,由于管理UITabBar可能比较麻烦,系统帮我们对其进行了封装,产生了简单好用的UITabBarController--标签视图控制器。
代码演示:
#import "AppDelegate.h" #import "FirstViewController.h" #import "SecondViewController.h" #import "ThirdViewController.h" #import "FourthViewController.h" @interface AppDelegate () @end @implementation AppDelegate -(void)dealloc{ [_window release]; [super dealloc]; } - (void)change{ [[UITabBar appearance]setTintColor:[UIColor greenColor]]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; FirstViewController *firstVC = [[FirstViewController alloc]init]; firstVC.tabBarItem.title = @"帅哥";//设置显示的文本 // firstVC.tabBarItem.badgeValue = @"爱好";//设置显示角标 [firstVC.button addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside]; firstVC.tabBarItem.image = [[UIImage imageNamed:@"iconfont-cool"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; //设置图标 /* 第一个参数为标签显示的文本 第二个参数正常状态下显示的图片 第三个参数选中状态下显示的图片 注意,图片想要正常显示,必须是png类型 */ SecondViewController *secondVC = [[SecondViewController alloc]init]; UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:@"何二" image:[UIImage imageNamed:@"iconfont-douyixia"] selectedImage:[UIImage imageNamed:@"iconfont-xu"]]; // 创建好标签之后指定为某一个视图控制器的标签 secondVC.tabBarItem = item; [item release]; //系统自带图标 ThirdViewController *thirdVC = [[ThirdViewController alloc]init]; UITabBarItem *systemItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:101]; thirdVC.tabBarItem = systemItem; [systemItem release]; /* 第一个参数显示文本 第二个参数正常状态下显示的图片 第三个参数标记值 */ FourthViewController *fourthVC = [[FourthViewController alloc]init]; UITabBarItem *commomItem = [[UITabBarItem alloc]initWithTitle:@"洪荒" image:[UIImage imageNamed:@"iconfont-zhu"] tag:102]; fourthVC.tabBarItem = commomItem; //一键换肤 [[UITabBar appearance]setTintColor:[UIColor redColor]]; UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:firstVC]; //创建标签视图控制器 UITabBarController *tabbarController = [[UITabBarController alloc]init]; //设置标签视图控制器需要管理的子视图控制器 tabbarController.viewControllers = @[naVC,secondVC,thirdVC,fourthVC]; // tabbarController.selectedIndex = 1; //指定window的根视图控制器为标签视图控制器 self.window.rootViewController = tabbarController; [firstVC release]; [secondVC release]; [thirdVC release]; [fourthVC release]; [naVC release]; [tabbarController release];
总之它可以用标签管理多个页面,非常的方便,很多软件都用到它。