标签:
一、多控制器
一个iOS的app很少只由一个控制器组成,除非这个app极其简单。当app中有多个控制器的时候,我们就需要对这些控制器进行管理
有多个view时,可以用一个大的view去管理1个或者多个小view,控制器也是如此,用1个控制器去管理其他多个控制器
比如,用一个控制器A去管理3个控制器B、C、D。控制器A被称为控制器B、C、D的“父控制器”;控制器B、C、D的被称为控制器A的“子控制器”
为了便于管理控制器,iOS提供了2个比较特殊的控制器
UINavigationController
UITabBarController
二、导航控制器
利用UINavigationController,可以轻松地管理多个控制器,轻松完成控制器之间的切换
导航控制器是一种头尾数的视图结构控制器
创建导航控制器必须有一个根视图控制器添加到导航控制器上
导航自带两个工具条: 1.导航条,默认显示
2.工具条,默认隐藏
*/
三、UINavigationController的使用步骤
(1)初始化UINavigationController
(2)设置UIWindow的rootViewController为UINavigationController
(3)根据具体情况,通过push方法添加对应个数的子控制器
1.标签导航代码
AppDelegate中
ViewController *yellowView = [[ViewController alloc] init];
RedViewController *redView = [[RedViewController alloc] init];
BlueViewController *blueView = [[BlueViewController alloc] init];
GreenViewController *greenView = [[GreenViewController alloc] init];
yellowView.tabBarItem.title = @"Yellow";
blueView.tabBarItem.title = @"Blue";
greenView.tabBarItem.title = @"Green";
//创建导航控制器
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:redView];
redView.navigationItem.title = @"导航首页";
redView.tabBarItem.title = @"Red";
navigation.tabBarItem.title = @"dao";
//创建标签栏控制器
UITabBarController *tabBarController = [[UITabBarController alloc] init];
//把创建好的界面加到标签栏控制器里面
[tabBarController setViewControllers:@[yellowView,navigation,blueView,greenView] animated:YES];
//把导航控制器设置为要显示的窗口的根视图
self.window.rootViewController = tabBarController;2.自定义标签栏
新建文件 UITabBarController类
_tempAry = [NSMutableArray array];
ViewControllerOne *vc1 = [[ViewControllerOne alloc] init];
ViewControllerTwo *vc2 = [[ViewControllerTwo alloc] init];
ViewControllerThree *vc3 = [[ViewControllerThree alloc] init];
ViewControllerFour *vc4 = [[ViewControllerFour alloc] init];
self.viewControllers = @[vc1,vc2,vc3,vc4];
//隐藏自带的标签栏
self.tabBar.hidden = YES;
//自定义标签背景
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"tabBar_bg_7@2x.png"];
imageView.frame = CGRectMake(0, self.view.frame.size.height-49, 320, 49);
[self.view addSubview:imageView];
//开启imageView与用户交互的权限
imageView.userInteractionEnabled = YES;
//添加标签栏上的按钮
float gap = (320 - 4*30)/5;
for (int i = 0; i < 4; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = i+11;
btn.frame = CGRectMake(gap+(gap+30)*i, (49-30)/2, 30, 30);
//设置按钮默认、选中时候的图片
UIImage *imageNormal = [UIImage imageNamed:[NSString stringWithFormat:@"tabBtn%i_1.png",i+1]];
UIImage *selected = [UIImage imageNamed:[NSString stringWithFormat:@"tabBtn%i_2.png",i+1]];
[btn setImage:imageNormal forState:UIControlStateNormal];
[btn setImage:selected forState:UIControlStateSelected];
[imageView addSubview:btn];
[_tempAry addObject:btn];
//设置按钮选中状态
if (btn.tag == 11) {
btn.selected = YES;
self.selectedIndex = 0;
}
//点击按钮,进入点击方法
[btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)clickBtn:(UIButton *)sender{
for (UIButton *nowBtn in _tempAry) {
nowBtn.selected = nowBtn.tag == sender.tag ? YES : NO;
// if (nowBtn.tag == sender.tag) {
// sender.selected = YES;
// }else{
// nowBtn.selected = NO;
// }
}
int index = sender.tag - 11;
self.selectedIndex = index;
}UI:导航控制器UINavigationController的使用
标签:
原文地址:http://blog.csdn.net/ytuzhangziyao/article/details/43201789