iOS开发之UITabBarController的创建和使用

UITabBarController是一个容器控制器,继承于UIViewController,一般情况下作为window的根视图控制器

UITabBarController自带一个UITabBar

首先创建一个UItabBarController

 UITabBarController* barTC =[[UITabBarControlleralloc]init];

作为window的根视图

  self.window.rootViewController= barTC;

 

设置标题为首页

 barTC.navigationItem.title= @"首页";

 

创建5个视图控制器

   FirstViewController * firstVC =[[FirstViewControlleralloc]init];

   SecondViewController * secondVC =[[SecondViewControlleralloc]init];

   ThirdViewController * thirdVC =[[ThirdViewControlleralloc]init];

   FourViewController * fourVC =[[FourViewControlleralloc]init];

 

   FiveViewController* fiveVC  =[[FiveViewControlleralloc]init];

再给5个视图添加导航控制器

 UINavigationController* navigationVC1 =[[UINavigationControlleralloc]initWithRootViewController:firstVC];

 UINavigationController * navigationVC2 =[[UINavigationControlleralloc]initWithRootViewController:secondVC];

 UINavigationController* navigationVC3 =[[UINavigationControlleralloc]initWithRootViewController:thirdVC];

    UINavigationController* navigationVC4 =[[UINavigationControlleralloc]initWithRootViewController:fourVC];

    UINavigationController* navigationVC5 =[[UINavigationControlleralloc]initWithRootViewController:fiveVC];

//UITabBarController的子视图控制器

    barTC.viewControllers=@[navigationVC1,navigationVC2,navigationVC3,navigationVC4,navigationVC5];


  firstVC.view.backgroundColor= [UIColorredColor];

   firstVC.tabBarItem.title= @"首页";

      //设置角标

   firstVC.tabBarItem.badgeValue= @"33";

 

 //UItabBar会对图片进行渲染,如果不想渲染的话就需要使用(UIImageRenderingModeAlwaysOriginal)。

   firstVC.tabBarItem.image= [[UIImageimageNamed:@"home.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

   secondVC.view.backgroundColor= [UIColorblackColor];

   secondVC.tabBarItem.title= @"发现";

   secondVC.tabBarItem.image= [[UIImageimageNamed:@"find.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

   thirdVC.view.backgroundColor= [UIColorwhiteColor];

   thirdVC.tabBarItem.title= @"设置";

   thirdVC.tabBarItem.image= [[UIImageimageNamed:@"setting.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

   fourVC.view.backgroundColor= [UIColorgrayColor];

   fourVC.tabBarItem.title= @"购物";

   fourVC.tabBarItem.image= [[UIImageimageNamed:@"shopping.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

   fiveVC.view.backgroundColor= [UIColorgreenColor];

   fiveVC.tabBarItem.title= @"联系人";

 

   fiveVC.tabBarItem.image= [UIImageimageNamed:@"contact.png"];

 

//

//   firstVC.tabBarItem = [[[UITabBarItemalloc]initWithTabBarSystemItem:(UITabBarSystemItemBookmarks)tag:1]autorelease];

//   firstVC.tabBarItem.badgeValue =@"23";

//   secondVC.tabBarItem  =[[[UITabBarItemalloc]initWithTabBarSystemItem:(UITabBarSystemItemContacts)tag:2]autorelease];

//   thirdVC.tabBarItem = [[[UITabBarItemalloc]initWithTabBarSystemItem:(UITabBarSystemItemDownloads)tag:3]autorelease] ;

//   fourVC.tabBarItem = [[[UITabBarItemalloc]initWithTitle:@"发现" image:[UIImage imageNamed:@"find.png"]selectedImage:[UIImageimageNamed:@"map.png"]]autorelease];

//   fiveVC.tabBarItem = [[[UITabBarItemalloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"home.png"]selectedImage:[UIImage imageNamed:nil]]autorelease];

//   SixViewController * sixVC = [[SixViewControlleralloc]init];

//   sixVC.tabBarItem = [[[UITabBarItemalloc]initWithTitle:@"猴子" image:[UIImage imageNamed:@"monkey.png"]selectedImage:nil]autorelease];

效果图如下:



三大视图控制器组成的框架结构--UItabBarController、UINavigationController、UITableViewController

    第一种--UINavigationController管理UITabBarController,这种结构UINavigationBar上面的内容是不变的。

    第二种结构---UItabBarController管理UINavigationController,这种结构每一个具体的视图控制器可以设置自己在navigationBar上面的内容

    主要事项:1.如果要给tabBarItem,应该给对应的navigationcontroller设置

 

    2.如果设置navigationBarItem,应该给具体的视图控制器设置。一般情况下会在视图控制器的内部方法里面设置

UITabBarControllerDelegate协议里面有如下方法,要定义代理对象为自身   barTC.delegate = self;

- (void)tabBarController:(UITabBarController *)tabBarControllerdidSelectViewController:(UIViewController *)viewController


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值