tab bar controller 代码使用例子


  要自定义UITabBarController,首先我们必须了解UITabBarController结构与其各个相关类的关系(TabBarController、TabBar、TabButton及ViewController)。其中,TabButton是一个双态的Button(选中和未选中),行为和CheckBox、RadioButton类似。TabBar是TabButton的容器,负责TabButton的排布和互斥,保证同时只有一个Button为选中态。TabBarController包含了TabBar,并管理这一个ViewController的栈,在TabBar上的按钮点击时对栈上的ViewController位置进行相应的调整,从而保持TabBar和ViewController栈之间的一致性。

tab bar controller 

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColorwhiteColor];
    
    //生成各个视图控制器
    a* aa = [[a alloc]init];
    b* bb = [[b alloc]init];
    c* cc = [[c alloc]init];
    d* dd = [[d alloc]init];
    e* ee = [[e alloc]init];
    //加入一个数组    
    NSArray* controllerArray = [[NSArray alloc]initWithObjects:aa,bb,cc,dd,ee ,nil];
    //创建UITabBarController控制器  (定义在.h中,否在开启arc会自动释放掉,内存访问出错)
    tabBarController = [[UITabBarControlleralloc]init];
    //    设置委托
    tabBarController.delegate = self;
    //设置UITabBarController控制器的viewControllers属性为我们之前生成的数组controllerArray
    tabBarController.viewControllers = controllerArray;
    //默认选择第1个视图选项卡(索引从0开始的)
    tabBarController.selectedIndex = 0;
//设置TabBarItem的标题与图片
    [(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:0] setTitle:@"aa"];  
    [(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:0] setImage:[UIImageimageNamed:@"1517.png"]];  //30 * 30
    [(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:1] setTitle:@"bb"];
    [(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:1] setImage:[UIImageimageNamed:@"light.png"]];
    [(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:2] setTitle:@"cc"];
    [(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:2] setImage:[UIImageimageNamed:@"podcaste.png"]];
    [(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:3] setTitle:@"dd"];
    [(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:3] setImage:[UIImageimageNamed:@"setting.png"]];
    [(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:4] setTitle:@"ee"];
    [(UITabBarItem *)[tabbarController., tabBar.itemsobjectAtIndex:4] setImage:[UIImageimageNamed:@"unlock.png"]];
    //读取
    UIViewController* activeController = tabBarController.selectedViewController;
    if(activeController == aa){
        //
    }
    //    把tabBarController的view作为子视图添加到window
    [self.window addSubview:tabBarController.view];
    
    //定制按钮
    /*NSMutableArray* customzableViewControllers = [[NSMutableArray alloc]init];
     [customzableViewControllers addObject:musicList];
     [customzableViewControllers addObject:currentPlay];
     [customzableViewControllers addObject:favourite];
     tabBarController.customizableViewControllers = customzableViewControllers;
     
     tabBarController.customizableViewControllers = nil;
     */
    //
    [self.windowmakeKeyAndVisible];
    returnYES;
}

 标签页内的项目切换  :
 tabBarController.selectedIndex = 1;//(从0开始)
即跳转到第二个子标签页面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值