iPhone/iOS UIViewController之UITabBarController

一.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];


同样可以用nib

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];
   }
}

4.跟踪tab变化的方法

设置tabBarController的delegate,delegate实现UITabBarControllerDelegate协议


5.tab右上角的提示

badgeValue:右上角一个提示的文字,吸引用户来点击




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值