UITabBarController及三种控制器的…

来源:互联网 发布:金融教学软件 编辑:程序博客网 时间:2024/06/05 17:54
         第一部分:UITabBarController 标签视图控制器
 UITabBarController 标签视图控制器继承自UIViewController,一经创建自带一个视图,这个视图上有两个控件contentView 和tabBar,是用来管理多个单视图控制器,他管理的多个单视图控制器之间是并列关系,同时存在,但是相互之间没有太大关联
   
    UITabBarController  管理的视图控制器对象自带的View只有当它第一次出现的时候会创建,以后就不再创建,所以标签视图控制器管理的视图控制器之间是并列存在的
所需背景图片
本节所需素材下载http://pan.baidu.com/s/1kTmzYTp
  ==========================================================
AppDelegate.m
#import "FirstViewController.h"
#import
"SecondViewController.h"
#import
"ThirdViewController.h"
#import
"FifthViewController.h"
#import
"ForthViewController.h"
#import "SixthViewController.h"
   //firstVC
   
FirstViewController*firstVC =[[FirstViewControlleralloc]init];
   
//配置标签栏标题
   firstVC.
tabBarItem.title = @"FirstVC";
   
//配置标签图片
   firstVC.
tabBarItem.image = [UIImage imageNamed:@"24-gift.png"];
   
//配置右上角的角标
   firstVC.
tabBarItem.badgeValue = @"99+";
   
   
//secondVC
   
SecondViewController*secondVC =[[SecondViewControlleralloc]init];
    
//配置标签栏标题
   secondVC.
tabBarItem.title = @"SecondVc";
   
//配置标签图片
   secondVC.
tabBarItem.image = [UIImage imageNamed:@"18-envelope.png"];
   
//配置右上角的角标
   secondVC.
tabBarItem.badgeValue = @"36";
   
//tabar的唯一标示
    secondVC.
tabBarItem.tag = 101;
   
   
//thirdVC
   
ThirdViewController*thirdVC =[[ThirdViewControlleralloc]init];
   
//配置标签栏标题
   thirdVC.
tabBarItem.title = @"ThirdVC";
   
//配置标签图片
   thirdVC.
tabBarItem.image = [UIImage imageNamed:@"82-dogpaw.png"];
   
//配置右上角的角标
   thirdVC.
tabBarItem.badgeValue = @"";
   
   
//forthVC
   
ForthViewController*forthVC=[[ForthViewControlleralloc]init];
   
//配置标签栏标题
   forthVC.
tabBarItem.title = @"ForthVC";
   
//配置标签图片
   forthVC.
tabBarItem.image = [UIImage imageNamed:@"27-planet.png"];
   
//配置右上角的角标
   forthVC.
tabBarItem.badgeValue = @"任意";
   
   
FifthViewController*fifthVC =[[FifthViewControlleralloc]init];
   fifthVC.
tabBarItem.title = @"FifthVC";
   fifthVC.
tabBarItem.image = [UIImage imageNamed:@"34-coffee.png"];
   
   
SixthViewController*sixthVC =[[SixthViewControlleralloc]init];
   sixthVC.
tabBarItem.title = @"SixthVC";
   sixthVC.
tabBarItem.image = [UIImage imageNamed:@"29-heart.png"];
   
   
UITabBarController*tbController =[[UITabBarControlleralloc]init];
   
//准备一个数组存放视图控制器
   
NSArray*controllers =@[firstVC,secondVC,thirdVC,forthVC,fifthVC,sixthVC];
   
   [firstVC
release];
   [secondVC
release];
   [thirdVC
release];
   [forthVC
release];
   [fifthVC
release];
   [sixthVC
release];
   
UITabBarController及三种控制器的组合使用

   //1.配置所管理的单视图
   tbController.
viewControllers = controllers;
   
//2.配置标签栏颜色
   tbController.
tabBar.barTintColor = [UIColor blackColor];
   
//3.配置内容渲染颜色
   tbController.
tabBar.tintColor = [UIColor redColor];
   
//4.配置标签栏的图片
   
//标签栏的高度 49
   tbController.
tabBar.backgroundImage = [UIImage imageNamed:@"01"];
   
//5.配置默认选中的标签
   
//根据下标选择
//   tbController.selectedIndex = 2;
   
//根据视图控制器来选择
//   tbController.selectedViewController = forthVC;
   
   
//6.配置代理属性
   tbController.delegate = self;
   //将标签视图控制器指定为window的根视图控制器
   
self.window.rootViewController=tbController;
   [tbController release];
   returnYES;
}

//当切换标签的时候触发,询问当前的标签是否可以选中
- (
BOOL)tabBarController:(UITabBarController*)tabBarControllershouldSelectViewController:(UIViewController*)viewControllerNS_AVAILABLE_IOS(3_0){
//   if(tabBarController.selectedIndex == 1 ){
//       return NO;
//   }else{
//       return YES;
//   }      会点死,不能使用
   
   //tabBarController指的是当前的代理委托者
   
//ViewController指的是将要选中的视图
//   if(101 ==viewController.tabBarItem.tag){
//       return NO;
//   }
       returnYES;
}
效果:
UITabBarController及三种控制器的组合使用


//触发时机标签选中的时候会触发
- (
void)tabBarController:(UITabBarController*)tabBarControllerdidSelectViewController:(UIViewController*)viewController{
   
//将选中标签的角标取出,选中那一个去掉哪一个
   viewController.
tabBarItem.badgeValue = nil;
}
效果:
UITabBarController及三种控制器的组合使用



——————————————————————————————
FirstViewController.m
   使用UITabBarController管理的视图的之间的切换,走的哪些方法?
A-->B :B将要出现-->A将要消失-->A已经消失-->B已经出现
B-->A :A将要出现-->B将要消失-->B已经消失-->A已经出现
-(void)viewWillAppear:(BOOL)animated{
   
NSLog(@"firstVC将要出现");
}
- (
void)viewDidAppear:(BOOL)animated{
   
NSLog(@"firstVC已经出现");
}
- (
void)viewWillDisappear:(BOOL)animated{
   
NSLog(@"firstVC将要消失");
}
- (
void)viewDidDisappear:(BOOL)animated{
   
NSLog(@"firstVC已经消失");
}
 SecondViewController.h和FirstViewController.h 一样只需把背景颜色设置一下即可ThirdViewController.m、FifthViewController.m、ForthViewController.m、SixthViewController.m不做详细介绍只需继承自UIViewController设置背景颜色即可。
——————————————————————————————
    UITabBarController 管理的标签超过5个的时候,会自动生成more按钮,more对应视图控制器是导航控制器,多出来的视图都被导航控制器管理
  UITabBarController及三种控制器的组合使用

===================================================

   第二部分:MixUserControllers 控制器的混合使用
UITabBarController、UINavigationController、UITableViewController的混合使用

 UITabBarController素材下载 http://pan.baidu.com/s/1kTvnyT5
AppDelegate.m
//创建标签视图控制器对象
   
NBViewController*nbVC =[[NBViewControlleralloc]init];
   
//将标签视图控制器对象指定为window的根视图控制器
   
self.window.rootViewController= nbVC;
   [nbVC release];
——————————————————————————————
NBViewController.m
#import"ChatViewController.h"
#import
"ContactViewController.h"
#import
"FindViewController.h"
#import"MineViewController.h"
//调用自定义视图控制器
   [
self configureViewController];
//修改所有导航跳的颜色---->类似于一件换肤
   [[UINavigationBarappearance]setBarTintColor:[UIColorlightGrayColor]];

   [[UITabBarappearance]setBarTintColor:[UIColorlightGrayColor]];
}
//自定义视图控制器
- (
void)configureViewController{
   
//消息
   
ChatViewController*chatVC =[[ChatViewControlleralloc]init];
   
UINavigationController*chatNC =[[UINavigationControlleralloc]initWithRootViewController:chatVC];
   
//chatVC.tableBarItem可以获取到距离它最近的标签栏
  
//chatVC.navigationItem 获取到距离它最近的导航条

   chatNC.
tabBarItem.image=[UIImageimageNamed:@"tabbar_mainframe@2x.png"];
//   chatNC.tabBarItem.title = @"微信";
//   //配置导航条title
//   chatVC.navigationItem.title = @"微信";
   
//可以同时设置距离它最近的导航条和标签栏的标题
   chatVC.
title=@"微信";

   //通讯录
   
ContactViewController*contactVC =[[ContactViewControlleralloc]init];
   
UINavigationController*contactNC =[[UINavigationControlleralloc]initWithRootViewController:contactVC];
   contactNC.
tabBarItem.image=[UIImageimageNamed:@"tabbar_contacts@2x.png"];
   contactVC.
title=@"通讯录";

   //发现
   
FindViewController*findVC =[[FindViewControlleralloc]init];
   
UINavigationController*findNC =[[UINavigationControlleralloc]initWithRootViewController:findVC];
    findNC.
tabBarItem.image=[UIImageimageNamed:@"tabbar_discover@2x"];
   findVC.
title=@"发现";
    
   //我的
   
MineViewController*mineVC =[[MineViewControlleralloc]init];
   
UINavigationController*mineNC =[[UINavigationControlleralloc]initWithRootViewController:mineVC];
   mineNC.
tabBarItem.image=[UIImageimageNamed:@"tabbar_me@2x.png"];
   mineVC.
title=@"我";
   
   
//配置标签视图控制器管理视图的数组
   
NSArray*viewControllers= @[chatNC,contactNC,findNC,mineNC];
   
   
//释放
   [chatVC
release];
   [chatNC
release];
   [contactVC
release];
   [contactNC
release];
   [findVC
release];
   [findNC
release];
   [mineVC
release];
   [mineNC
release];
   
   
//给标签视图控制器的viewControllers数组赋值
   
self.viewControllers=viewControllers;
//   self.tabBar.backgroundImage = [UIImageimageNamed:@"01"];
//   self.tabBar.barTintColor = [UIColorlightGrayColor];
}
——————————————————————————————————
继承自: UITableViewController
ChatViewController.m
#definekTableCell @"table-cell"
#import"DetailViewController.h"

   //注册cell
   [
self.tableViewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:kTableCell];
   
//1.访问到当前管理该视图的导航控制器(最近的)
   
self.navigationController;
   
//2.访问到当前管理该视图的标签栏控制器(最近)
   
self.tabBarController;
   
//3.访问到当前管理该视图控制器的导航控制器所管理偶的所有视图控制器
   
self.navigationController.viewControllers;
   
//4.访问到当前管理该视图控制器的标签视图控制器所管理的所有的视图控制器
   self.tabBarController.viewControllers;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
   return1;
}

- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{
   return10;
}

- (UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{
   UITableViewCell*cell =[tableView dequeueReusableCellWithIdentifier:kTableCellforIndexPath:indexPath];
   cell.textLabel.text=@"在和习大大聊天";
   returncell;
}

//点击cell会走的方法
- (
void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{
   
//创建detailViewController对象
   
//创建UITableViewController子类时可以指定自带的tableView的格式是plain还是group样式的;
   DetailViewController*detailVC =[[DetailViewControlleralloc]initWithStyle:(UITableViewStyleGrouped)];
需要注意的一点:显示和隐藏标签栏分别在push前后
   //跳转时是否隐藏标签栏
   self.hidesBottomBarWhenPushed=YES;
   [self.navigationControllerpushViewController:detailVCanimated:YES];
   
//跳转返回时显示标签栏
   
self.hidesBottomBarWhenPushed=NO;
   [detailVC release];
}
IOS中如何在多层界面之间显示与隐藏标签栏(UITabBar): http://blog.sina.com.cn/s/blog_814ecfa90102vx5o.html
————————————————————————————————
ChatViewController.m、ContactViewController.m、FindViewController.m、MineViewController.m同理继承自: UITableViewController创建没有设置内容
————————————————————————————————
最终效果:
UITabBarController及三种控制器的组合使用

==========================================================================
欢迎学习本文,未经博主许可,禁止转载!
0 0
原创粉丝点击