UITabBarController 标签视图控制器
1.UITabBarController继承自UIViewController本身自带一个视图,这个视图上有两个控件contentview和tabBar,是用来管理多个单视图控制器的多个视图控制器,它管理多个单视图控制器之间是并列的关系,同时存在,但是相互之间没有太大的关联
2.UITabBarController管理的标签超过五个的时候,会自动生成more按钮,more按钮对应的视图控制器是导航视图控制器,多出来的视图都被这个导航控制器管理
3.UITabBarController管理的视图控制器对象自带的View只有当它第一次出现的时候会创建,以后就不在创建了,所有标签视图控制器管理的视图控制器之间是并列存在的
属性
配置标签栏的标题
firstVC.tabBarItem.title
=
@"firstVC";
配置标签栏的图片
firstVC.tabBarItem.image
= [UIImage
imageNamed:@"01-refresh"];
配置右上角的角标
firstVC.tabBarItem.badgeValue
=
@"99+";
4.
1.创建对象
UITabBarController
*tbContoller = [[UITabBarController
alloc]init];
2.准备一个数组存放视图控制器
NSArray
*controllers =
@[firstVC,secondVC,thirdVC,forthVC,fifthVC,sixVC];
3.配置所管理的多个单视图控制器
tbContoller.viewControllers
= controllers;
4.配置标签栏的颜色
tbContoller.tabBar.barTintColor
= [UIColor
purpleColor];
5.配置内容的渲染颜色
tbContoller.tabBar.tintColor
= [UIColor
cyanColor];
6.配置标签栏的图片
标签栏的高度49
tbContoller.tabBar.backgroundImage
= [UIImage
imageNamed:@"1"];
7.配置默认选中的标签有两种方法
方法一
根据下标选择
tbContoller.selectedIndex
=2;
方法二 根据视图控制器选择
tbContoller.selectedViewController
= forthVC;
8.配置代理属性
tbContoller.delegate
=
self;
9.将标签视图控制器指定为window的根视图控制器
self.window.rootViewController = tbContoller;
[tbContoller release];
self.window.rootViewController = tbContoller;
[tbContoller release];
#pragma mark
代理方法
//1.当切换标签的时候出发,询问当前的标签是否可以选中
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0){
// viewController指的是将要选中的视图
// tabBarController指的是当前的代理委托者
// if(viewController.tabBarItem.tag == 101){
//1.当切换标签的时候出发,询问当前的标签是否可以选中
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0){
// viewController指的是将要选中的视图
// tabBarController指的是当前的代理委托者
// if(viewController.tabBarItem.tag == 101){
// return NO;
//}
return
YES;
}
//2.触发时机标签选中的时候会触发
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
// 将选中标签的角标去除,选择哪一个去除哪一个
viewController.tabBarItem.badgeValue = nil;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
// 将选中标签的角标去除,选择哪一个去除哪一个
viewController.tabBarItem.badgeValue = nil;
}
#########总结#############
/使用UItabBarController管理的视图之间的切换,走的哪些方法
//A-->B:B将要出现---》A将要消失---》A已经消失---》B已经出现
//B---A:A将要出现---》B将要消失--》B已经消失---》A已经出现
//A-->B:B将要出现---》A将要消失---》A已经消失---》B已经出现
//B---A:A将要出现---》B将要消失--》B已经消失---》A已经出现
微信界面的实现
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
[self configureViewController];
// 修改所有导航条的颜色 类似于一键换肤
[[UINavigationBar appearance] setBarTintColor:[UIColor lightGrayColor]];
// 修改所有标签栏的颜色
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
[self configureViewController];
// 修改所有导航条的颜色 类似于一键换肤
[[UINavigationBar appearance] setBarTintColor:[UIColor lightGrayColor]];
// 修改所有标签栏的颜色
[[UITabBar
appearance]
setBarTintColor:[UIColor
lightGrayColor]];
//修改所有标签栏的内容渲染颜色
[[UITabBar
appearance]
setTintColor:[UIColor
orangeColor]];
}
1.
消息界面
ChatViewController *chatVC = [[ChatViewController alloc]init];
UINavigationController *chatNC = [[UINavigationController alloc]initWithRootViewController:chatVC];
// chatVC.tabBarIrem 可以获取到离他最近的标签栏
// chatVC.navigationItem 获取到离他最近的导航条
// chatVC.tabBarItem.title = @"微信";
chatVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_mainframe@2x"];
// chatVC.navigationItem.title = @"微信";
chatVC.title = @"微信";//可以同时设置离他最近的导航条和标签栏的标题
ChatViewController *chatVC = [[ChatViewController alloc]init];
UINavigationController *chatNC = [[UINavigationController alloc]initWithRootViewController:chatVC];
// chatVC.tabBarIrem 可以获取到离他最近的标签栏
// chatVC.navigationItem 获取到离他最近的导航条
// chatVC.tabBarItem.title = @"微信";
chatVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_mainframe@2x"];
// chatVC.navigationItem.title = @"微信";
chatVC.title = @"微信";//可以同时设置离他最近的导航条和标签栏的标题
// chatVC.navigationController.navigationBar.barTintColor = [UIColor grayColor];
//自定义的视图控制器方法
- (void)configureViewController
- (void)configureViewController
{
通讯录
ContactViewController *contactVC = [[ContactViewController alloc]init];
UINavigationController *contactNC = [[UINavigationController alloc]initWithRootViewController:contactVC];
contactVC.title =@"通讯录";
contactVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_contacts@2x"];
ContactViewController *contactVC = [[ContactViewController alloc]init];
UINavigationController *contactNC = [[UINavigationController alloc]initWithRootViewController:contactVC];
contactVC.title =@"通讯录";
contactVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_contacts@2x"];
发现
FindViewController *findVC = [[FindViewController alloc]init];
UINavigationController *findNC = [[UINavigationController alloc]initWithRootViewController:findVC];
findVC.title = @"发现";
FindViewController *findVC = [[FindViewController alloc]init];
UINavigationController *findNC = [[UINavigationController alloc]initWithRootViewController:findVC];
findVC.title = @"发现";
findVC.tabBarItem.image
= [UIImage
imageNamed:@"tabbar_discover@2x"];
我的
MineViewController *mineVC = [[MineViewController alloc]init];
UINavigationController *mineNC = [[UINavigationController alloc]initWithRootViewController:mineVC];
mineVC.title = @"我";
mineVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_me@2x"];
// self.tabBar.barTintColor = [UIColor lightGrayColor];
MineViewController *mineVC = [[MineViewController alloc]init];
UINavigationController *mineNC = [[UINavigationController alloc]initWithRootViewController:mineVC];
mineVC.title = @"我";
mineVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_me@2x"];
// self.tabBar.barTintColor = [UIColor lightGrayColor];
配置标签视图控制器管理视图的数组
NSArray *viewControllers = @[chatNC,contactNC,findNC,mineNC];
[chatNC release];
[chatVC release];
[contactNC release];
[contactVC release];
[findNC release];
[findVC release];
[mineNC release];
[mineVC release];
// 给标签视图控制器的ViewController数组赋值
NSArray *viewControllers = @[chatNC,contactNC,findNC,mineNC];
[chatNC release];
[chatVC release];
[contactNC release];
[contactVC release];
[findNC release];
[findVC release];
[mineNC release];
[mineVC release];
// 给标签视图控制器的ViewController数组赋值
self.viewControllers
= viewControllers;
}
2.chatViewContr.m中写
#########################################################################
1.访问到当前管理该视图控制器的导航控制器(最近的)####
self.navigationController;
// 2.访问到当前管理该视图控制器的标签视图控制器(最近的)####
self.tabBarController;
// 3.访问到当前管理该视图控制器的导航控制器所管理的所有视图控制器
self.navigationController.viewControllers;
// 4.访问到当前管理该视图控制器的标签视图控制器所管理的所有的视图控制器
self.tabBarController.viewControllers;
// 注册cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTabelViewCell];
self.navigationController;
// 2.访问到当前管理该视图控制器的标签视图控制器(最近的)####
self.tabBarController;
// 3.访问到当前管理该视图控制器的导航控制器所管理的所有视图控制器
self.navigationController.viewControllers;
// 4.访问到当前管理该视图控制器的标签视图控制器所管理的所有的视图控制器
self.tabBarController.viewControllers;
// 注册cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTabelViewCell];
//点击cell会走的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 创建UItableViewController子类时可以指定自带的tableView的格式是plain还是group样式的
DetailViewController *detailVC = [[DetailViewController alloc]initWithStyle:(UITableViewStyleGrouped)];
// 跳转时是否隐藏标签栏
self.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:detailVC animated:YES];
self.hidesBottomBarWhenPushed = NO;
[detailVC release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 创建UItableViewController子类时可以指定自带的tableView的格式是plain还是group样式的
DetailViewController *detailVC = [[DetailViewController alloc]initWithStyle:(UITableViewStyleGrouped)];
// 跳转时是否隐藏标签栏
self.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:detailVC animated:YES];
self.hidesBottomBarWhenPushed = NO;
[detailVC release];
}
Block 传值
FirstViewController 和 SecondViewController 继承UIViewController
#warning Block
传值
#warning 第一步 定义给block的类型起别名 在secondVC.h中写
typedef void (^passBlock)(NSString *);
#warning 第一步 定义给block的类型起别名 在secondVC.h中写
typedef void (^passBlock)(NSString *);
#warning 第二步 定义block类型的变量
在secondVC.h中写
block变量存在于栈区,用copy修饰后就会存放在堆区,从而保证block块不会丢失,申请了内存就要释放内存
@property(nonatomic,copy)passBlock
passValue;
- (void)handlePush:(UIButton
*)button
{
{
SecondViewController *secondVC = [[SecondViewController
alloc]init];
#warning 第三步 push之前给block赋值
在firstVC.m中写
__block
typeof(self) weakSelf =
self;
secondVC.passValue = ^(NSString *string)
secondVC.passValue = ^(NSString *string)
{
在block块内给控件赋值
weakSelf.textField.text
= string;
};
[self.navigationController
pushViewController:secondVC
animated:YES];
[secondVC
release];
}
- (void)handlePop:(UIButton
*)pop
{
self.testBlock();//block的调用
#warning 第四步 pop之前调用block块 在secondVC.m中写
self.passValue(self.textField.text);
[self.navigationController popViewControllerAnimated:YES];
}
{
self.testBlock();//block的调用
#warning 第四步 pop之前调用block块 在secondVC.m中写
self.passValue(self.textField.text);
[self.navigationController popViewControllerAnimated:YES];
}