一.TabBarController的主要参数以及结构
主要参数有
viewControllers: tab bar statck
customizableViewController: custom一部分tabbarItem
selectedViewController:当前选择中的viewController
moreNavigationController:当tabbarItem过多,系统则会自动生成一个更多的item,可以通过该参数获得所需要的信息
二.创建Tab Bar,管理显示tab bar items
1.代码方式创建
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[[FCDemoViewController alloc] initWithNibName:@"FCDemoViewController" bundle:nil] autorelease]; FirstViewController *first= [[FirstViewController alloc]init]; first.tabBarItem.title=@"first"; first.tabBarItem.image=[UIImage imageNamed:@"1.jpg"]; //也可以用下面方式声明barItem UITabBarItem* itm=[[UITabBarItem alloc]initWithTitle:@"first" image:[UIImage imageNamed:@"1.jpg"] tag:2000]; first.tabBarItem=itm; first.tabBarItem.badgeValue=@"a"; self.viewController.tabBarItem.title=@"root"; self.viewController.tabBarItem.image=[UIImage imageNamed:@"book-prev1.png"]; UITabBarController *tab=[[UITabBarController alloc]init]; tab.viewControllers=[NSArray arrayWithObjects:self.viewController,first, nil]; self.window.rootViewController = tab; [self.window makeKeyAndVisible];
2.运行时管理tabbar
-(void) setViewControllers:animated;
3.禁止某个tab选中
设置tabBarController的delegate对象,并且实现-(BOOL)tabBarController:shouldSelectViewController:方法,返回no则当前这个选择无效。
下面代码是个登录界面,在没有登录成功情况下,只有点击第一个tab是有效的
- (IBAction)processUserInformation:(id)sender { // Call some app-specific method to validate the user data. // If the custom method returns YES, remove the tab. if ([self userDataIsValid]) { NSMutableArray* newArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers]; [newArray removeObject:self]; [self.tabBarController setViewControllers:newArray animated:YES]; } }
设置tabBarController的delegate,delegate实现UITabBarControllerDelegate协议
5.tab右上角的提示
badgeValue:右上角一个提示的文字,吸引用户来点击