【iOS基础控件 15 - 2 】 UITabBarController 控制器管理

A.需求
1.类似QQ、微信顶部或者底部的窗口转换导航条
2.给每个页面添加相应内容
 
B.UITabBarController
1.基本概念:
(1)内容高度
iOS7之前内容高度为:屏幕高度 - 状态栏高度44 - 底部导航条高度49
iOS7及之后,内容高度为整个屏幕
Image(254)
 
 
2.使用代码创建初始化
(1)创建一个UITabBarController
UITabBarController的使用步骤
  • 初始化UITabBarController
  • 设置UIWindow的rootViewController为UITabBarController
  • 根据具体情况,通过addChildViewController方法添加对应个数的子控制器
 
 
(2)添加子控制器
UITabBarController添加控制器的方式有2种
  • 添加单个子控制器
- (void)addChildViewController:(UIViewController *)childController;

  • 设置子控制器数组
@property(nonatomic,copy) NSArray *viewControllers;
 
 
如果UITabBarController有N个子控制器,那么UITabBar内部就会有N个UITabBarButton作为子控件
如果UITabBarController有4个子控制器,那么UITabBar的结构大致如下图所示
Image(255)
 
 
AppDelegate:
复制代码
 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 2     // Override point for customization after application launch.
 3    
 4     // 设置window
 5     self.window = [[UIWindow alloc] init];
 6     self.window.frame = [[UIScreen mainScreen] bounds];
 7     self.window.backgroundColor = [UIColor grayColor];
 8     [self.window makeKeyAndVisible];
 9    
10    
11     // 设置一个UITabBarController
12     UITabBarController *tabBarController = [[UITabBarController alloc] init];
13     self.window.rootViewController = tabBarController;
14    
15     // 添加子控制器
16     UIViewController *c1 = [[UIViewController alloc] init];
17     c1.view.backgroundColor = [UIColor redColor];
18 //    [tabBarController addChildViewController:c1];
19    
20     UIViewController *c2 = [[UIViewController alloc] init];
21     c2.view.backgroundColor = [UIColor blueColor];
22 //    [tabBarController addChildViewController:c2];
23    
24     UIViewController *c3 = [[UIViewController alloc] init];
25     c3.view.backgroundColor = [UIColor greenColor];
26    
27     tabBarController.viewControllers = @[c1,c2,c3];
28    
29    
30     return YES;
31 }
复制代码
 
Image(256)
 
 
(3)设置导航条选项样式
UITabBarButton里面显示什么内容,由对应子控制器的tabBarItem属性决定

EN001bbb7c

UITabBarItem有以下属性影响着UITabBarButton的内容
标题文字
@property(nonatomic,copy) NSString *title;

图标
@property(nonatomic,retain) UIImage *image;

选中时的图标
@property(nonatomic,retain) UIImage *selectedImage;

提醒数字
@property(nonatomic,copy) NSString *badgeValue;
 
AppDelegate:
复制代码
 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 2     // Override point for customization after application launch.
 3    
 4     // 设置window
 5     self.window = [[UIWindow alloc] init];
 6     self.window.frame = [[UIScreen mainScreen] bounds];
 7     self.window.backgroundColor = [UIColor grayColor];
 8     [self.window makeKeyAndVisible];
 9    
10    
11     // 设置一个UITabBarController
12     UITabBarController *tabBarController = [[UITabBarController alloc] init];
13     self.window.rootViewController = tabBarController;
14    
15     // 添加子控制器
16     UIViewController *c1 = [[UIViewController alloc] init];
17     c1.view.backgroundColor = [UIColor redColor];
18     c1.tabBarItem.title = @"红色";
19 //    [tabBarController addChildViewController:c1];
20    
21     UIViewController *c2 = [[UIViewController alloc] init];
22     c2.view.backgroundColor = [UIColor blueColor];
23     c2.tabBarItem.title = @"蓝色";
24 //    [tabBarController addChildViewController:c2];
25    
26     UIViewController *c3 = [[UIViewController alloc] init];
27     c3.view.backgroundColor = [UIColor greenColor];
28     c3.tabBarItem.title = @"绿色";
29    
30     tabBarController.viewControllers = @[c1,c2,c3];
31    
32    
33     return YES;
34 }
复制代码
 
Image(257)
 
3.使用storyboard创建初始化
(1)拖入UITabBarController
(2)拖入N个UIViewController待用
(3)按照顺序连线,设置UITabBarController的子控制器
Image(258)
 
 
Image(259)
 
(4)设置子控制器的TabBarItem
Image(260)
 
out:
Image(261)
 
 
注意:Selected Image不设置,系统就会自动将原图编程蓝色作为 Selected Image
 
4.子控制器的生命周期
(1)单个子控制器的生命周期和普通的控制器生命周期一样
生命周期方法
 
复制代码
 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     NSLog(@"%@ - viewDidLoad", self.class);
 4 }
 5 
 6 - (void)viewWillAppear:(BOOL)animated {
 7     [super viewWillAppear:animated];
 8     NSLog(@"%@ - viewWillAppear", self.class);
 9 }
10 
11 - (void)viewDidAppear:(BOOL)animated {
12     [super viewDidAppear:animated];
13     NSLog(@"%@ - viewDidAppear", self.class);
14 }
15 
16 - (void) viewWillDisappear:(BOOL)animated {
17     [super viewWillDisappear:animated];
18     NSLog(@"%@ - viewWillDisappear", self.class);
19 }
20 
21 - (void)viewDidDisappear:(BOOL)animated {
22     [super viewDidDisappear:animated];
23     NSLog(@"%@ - viewDidDisappear", self.class);
24 }
25 
26 
27 - (void)didReceiveMemoryWarning {
28     [super didReceiveMemoryWarning];
29     NSLog(@"%@ - didReceiveMemoryWarning", self.class);
30 }
31 
32 - (void)viewWillUnload {
33     [super viewWillUnload];
34     NSLog(@"%@ - viewWillUnload", self.class);
35 }
36 
37 - (void)viewDidUnload {
38     [super viewDidUnload];
39     NSLog(@"%@ - viewDidUnload", self.class);
40 }
复制代码
 
(2)两个子控制器切换的生命周期
a.出现第一个子控制器view
2014-12-27 21:11:44.338 UITabBarControllerByStoryboard[22567:590996] OneViewController - viewDidLoad
2014-12-27 21:11:44.340 UITabBarControllerByStoryboard[22567:590996] OneViewController - viewWillAppear
2014-12-27 21:11:44.407 UITabBarControllerByStoryboard[22567:590996] OneViewController - viewDidAppear
 
b.切换到第二个控制器view
2014-12-27 21:12:10.042 UITabBarControllerByStoryboard[22567:590996] TwoViewController - viewDidLoad
2014-12-27 21:12:10.042 UITabBarControllerByStoryboard[22567:590996] TwoViewController - viewWillAppear
2014-12-27 21:12:10.042 UITabBarControllerByStoryboard[22567:590996] OneViewController - viewWillDisappear
2014-12-27 21:12:10.054 UITabBarControllerByStoryboard[22567:590996] OneViewController - viewDidDisappear
2014-12-27 21:12:10.055 UITabBarControllerByStoryboard[22567:590996] TwoViewController - viewDidAppear
 
 
C.App框架
在每个子控制器之前再包装上一个NavigationController
Image(262)
 
 
Image(263)
 
 
Image(264)
posted @ 2015-02-09 16:54  kengsir  阅读(583)  评论(0编辑  收藏  举报