• iOS UI-标签控制器(UITabBarController)


     1 #import "AppDelegate.h"
     2 #import "FirstViewController.h"
     3 #import "SecondViewController.h"
     4 
     5 @interface AppDelegate ()
     6 
     7 @end
     8 
     9 @implementation AppDelegate
    10 
    11 
    12 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    13     
    14     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    15     self.window.backgroundColor = [UIColor whiteColor];
    16     
    17     //创建子控制器
    18     FirstViewController *first = [[FirstViewController alloc] init];
    19     first.tabBarItem.title = @"主页";
    20     first.tabBarItem.image = [UIImage imageNamed:@"home.png"];
    21     first.tabBarItem.badgeValue = @"123";
    22     
    23     SecondViewController *second = [[SecondViewController alloc] init];
    24     second.tabBarItem.title = @"设置";
    25     second.tabBarItem.image = [UIImage imageNamed:@"setting.png"];
    26 
    27     //创建标签控制器
    28     UITabBarController *tabCtr = [[UITabBarController alloc] init];
    29     
    30     NSArray *viewControllerArr = [NSArray arrayWithObjects:first,second, nil];
    31     
    32     tabCtr.viewControllers = viewControllerArr;
    33     
    34     self.window.rootViewController = tabCtr;
    35     
    36     [self.window makeKeyAndVisible];
    37     
    38     return YES;
    39 }
    40 
    41 
    42 #import "FirstViewController.h"
    43 
    44 @interface FirstViewController ()
    45 
    46 @end
    47 
    48 @implementation FirstViewController
    49 
    50 - (void)viewDidLoad {
    51     [super viewDidLoad];
    52     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, self.view.frame.size.width-200, 50)];
    53     label.backgroundColor = [UIColor whiteColor];
    54     label.textAlignment = NSTextAlignmentCenter;
    55     label.text = @"第一个视图";
    56     [self.view addSubview:label];
    57     
    58     self.view.backgroundColor = [UIColor cyanColor];
    59 }
    60 
    61 - (void)didReceiveMemoryWarning {
    62     [super didReceiveMemoryWarning];
    63     // Dispose of any resources that can be recreated.
    64 }
    65 
    66 @end
    67 
    68 
    69 
    70 #import "SecondViewController.h"
    71 
    72 @interface SecondViewController ()
    73 
    74 @end
    75 
    76 @implementation SecondViewController
    77 
    78 - (void)viewDidLoad {
    79     [super viewDidLoad];
    80     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, self.view.frame.size.width-200, 50)];
    81     label.backgroundColor = [UIColor whiteColor];
    82     label.textAlignment = NSTextAlignmentCenter;
    83     label.text = @"第二个视图";
    84     [self.view addSubview:label];
    85     
    86     self.view.backgroundColor = [UIColor purpleColor];
    87 }
    88 
    89 - (void)didReceiveMemoryWarning {
    90     [super didReceiveMemoryWarning];
    91     // Dispose of any resources that can be recreated.
    92 }
    93 
    94 @end

    一、简单介绍

    UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型的例子就是QQ、微信等应⽤。

    二、UITabBarController的使用

    1.使用步骤:

    (1)初始化UITabBarController

    (2)设置UIWindow的rootViewController为UITabBarController

    (3)创建相应的子控制器(viewcontroller)

    (4)把子控制器添加到UITabBarController

    2.代码示例

    新建一个空的文件,在Application的代理中编码

    YYAppDelegate.m文件

     1 //
     2 //  YYAppDelegate.m
     3 //  01-UITabBar控制器基本使用
     4 //
     5 //  Created by 孔医己 on 14-6-7.
     6 //  Copyright (c) 2014年 itcast. All rights reserved.
     7 //
     8 
     9 #import "YYAppDelegate.h"
    10 
    11 @implementation YYAppDelegate
    12 
    13 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    14 {
    15     //1.创建Window
    16     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    17     self.window.backgroundColor = [UIColor whiteColor];
    18     
    19     //a.初始化一个tabBar控制器
    20     UITabBarController *tb=[[UITabBarController alloc]init];
    21     //设置控制器为Window的根控制器
    22     self.window.rootViewController=tb;
    23     
    24     //b.创建子控制器
    25     UIViewController *c1=[[UIViewController alloc]init];
    26     c1.view.backgroundColor=[UIColor grayColor];
    27     c1.view.backgroundColor=[UIColor greenColor];
    28     c1.tabBarItem.title=@"消息";
    29     c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];
    30     c1.tabBarItem.badgeValue=@"123";
    31     
    32     UIViewController *c2=[[UIViewController alloc]init];
    33     c2.view.backgroundColor=[UIColor brownColor];
    34     c2.tabBarItem.title=@"联系人";
    35     c2.tabBarItem.image=[UIImage imageNamed:@"tab_buddy_nor"];
    36     
    37     UIViewController *c3=[[UIViewController alloc]init];
    38     c3.tabBarItem.title=@"动态";
    39     c3.tabBarItem.image=[UIImage imageNamed:@"tab_qworld_nor"];
    40     
    41     UIViewController *c4=[[UIViewController alloc]init];
    42     c4.tabBarItem.title=@"设置";
    43     c4.tabBarItem.image=[UIImage imageNamed:@"tab_me_nor"];
    44    
    45     
    46     //c.添加子控制器到ITabBarController中
    47     //c.1第一种方式
    48 //    [tb addChildViewController:c1];
    49 //    [tb addChildViewController:c2];
    50     
    51     //c.2第二种方式
    52     tb.viewControllers=@[c1,c2,c3,c4];
    53     
    54     
    55     //2.设置Window为主窗口并显示出来
    56     [self.window makeKeyAndVisible];
    57     return YES;
    58 }
    59 
    60 @end

    实现效果:

    三、重要说明

    1.UITabBar 

    下方的工具条称为UITabBar ,如果UITabBarController有N个子控制器,那么UITabBar内部就会有N 个UITabBarButton作为子控件与之对应。

    注意:UITabBarButton在UITabBar中得位置是均分的,UITabBar的高度为49。

    在上面的程序中,UITabBarController有4个子控制器,所以UITabBar中有4个UITabBarButton,UITabBar的结构⼤大致如下图所示:

     

     

    2.UITabBarButton 

    UITabBarButton⾥面显⽰什么内容,由对应子控制器的tabBarItem属性来决定 

     c1.tabBarItem.title=@"消息";
     c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];

    3.有两种方式可以往UITabBarController中添加子控制器 

    (1)[tb addChildViewController:c1];

    (2)tb.viewControllers=@[c1,c2,c3,c4];

    注意:展示的顺序和添加的顺序一致,和导航控制器中不同,展现在眼前的是第一个添加的控制器对应的View。

    二、UITabBarController在storyoard中得搭建
    1.新建一个项目,把storyboard中默认的控制器删除,拖UITab Bar Controller。
    2.创建viewcontroller,添加到UITab Bar Controller中去(连线)。
    注意点:连线的顺序就是将来显示的顺序,显示在眼前的为第一个连线的view。
    提示:控制器的界面对应的tabbarbutton和图片显示什么内容,由它的控制器确定。
    3.设置子控制器的UITabBar等信息。 
    4.运行效果
     
    三、UITabBarController的生命周期演示
    思路:新建三个控制器类来对控制器进行分别管理,重写内部的生命周期方法就可以了解UITabBarController内部管理机制。
     
    分析代码:
     1 //
     2 //  YYbaseViewController.m
     3 //  02-uitabbarcontroller
     4 //
     5 //  Created by 孔医己 on 14-6-8.
     6 //  Copyright (c) 2014年 itcast. All rights reserved.
     7 //
     8 
     9 #import "YYbaseViewController.h"
    10 
    11 @interface YYbaseViewController ()
    12 
    13 @end
    14 
    15 @implementation YYbaseViewController
    16 
    17 // 当控制器的view加载完毕就调用
    18 - (void)viewDidLoad
    19 {
    20     [super viewDidLoad];
    21     NSLog(@"%@ -  控制器的view加载完毕", [self class]);
    22 }
    23 
    24 // 控制器即将显示的时候调用
    25 - (void)viewWillAppear:(BOOL)animated
    26 {
    27     [super viewWillAppear:YES];
    28     NSLog(@"%@ -  控制器即将显示", [self class]);
    29 }
    30 
    31 // 控制器完全显示的时候调用
    32 - (void)viewDidAppear:(BOOL)animated
    33 {
    34     [super viewDidAppear:animated];
    35     NSLog(@"%@ -  控制器完全显示", [self class]);
    36 }
    37 
    38 // 控制器即将消失的时候调用
    39 - (void)viewWillDisappear:(BOOL)animated
    40 {
    41     [super viewWillDisappear:animated];
    42     NSLog(@"%@ -  控制器即将消失", [self class]);
    43 }
    44 // 控制器完全消失的时候调用
    45 - (void)viewDidDisappear:(BOOL)animated
    46 {
    47     [super viewDidDisappear:animated];
    48     NSLog(@"%@ -  控制器完全消失", [self class]);
    49 }
    50 
    51 - (void)viewWillUnload
    52 {
    53     [super viewWillUnload];
    54     NSLog(@"%@ -  view即将被销毁", [self class]);
    55 }
    56 
    57 - (void)viewDidUnload
    58 {
    59     [super viewDidUnload];
    60     NSLog(@"%@ -  view完全被销毁", [self class]);
    61 }
    62 
    63 - (void)dealloc
    64 {
    65     NSLog(@"%@",  [self class]);
    66 }
    67 
    68 @end
    (1)运行程序,打印输出为:
    说明:当把三个子控制器都添加给UITabBarController来管理后,当程序启动时它只会加载第一个添加的控制器的view。
    (2)点击联系人按钮,切换到第二个界面。打印输出为:
    说明:先把第一个view移开,再把新的view添加上去,但是第一个view并没有被销毁。
    (3)重新点击消息界面,打印如下:
    说明:先重新切换到消息界面,one控制器直接即将显示,没有进行加载证明了(2)中第一个view移除后并没有被销毁(因为它的控制器还存在,有一个强引用引用着它),且two的view移除后也没有被销毁。无论怎么切换,控制器和view都不会被销毁。
    UINavigationController和UITabBarController一个通过栈来管理,一个通过普通的数组来进行管理。
     
    补充说明:UITabBarController中的UITabBar实际高度为49.
    在Application的下面方法中打印UITabBar的frame进行查看。
    1 - (void)applicationDidBecomeActive:(UIApplication *)application
    2 {
    3     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    4     UITabBarController *tb=(UITabBarController*)self.window.rootViewController;
    5     NSLog(@"%@",NSStringFromCGRect(tb.tabBar.frame));
    6 }
  • 相关阅读:
    镜像劫持2
    镜像劫持2
    Windows核心编程 第十七章 -内存映射文件(下)
    Windows核心编程 第十七章 -内存映射文件(下)
    WindowsPE 第五章 导出表编程-1(枚举导出表)
    WindowsPE 第五章 导出表编程-1(枚举导出表)
    PowerShell-2.解决禁止本地执行脚本
    PowerShell-2.解决禁止本地执行脚本
    PowerShell-1.入门及其常用
    CodeForces B. Creating the Contest
  • 原文地址:https://www.cnblogs.com/oc-bowen/p/5092601.html
Copyright © 2020-2023  润新知