6.5 UITabBarController

1、UITabBarController的方法及属性介绍

1 创建一个UITabBarController对象

2 创建tabbarcontroller中每一个tab对应的要显示的对象

3 通过UITabBarController的viewController属性将要显示的所有content viewcontroller添加到UITabBarController中

4 通过设置UITabBarController对象为window.rootViewController,然后显示window



#pragma mark - 设置根控制器
    UITabBarController *tabController = [[UITabBarController alloc]init];
    self.window.rootViewController = tabController;

    //创建控制器
    MainViewController *mainVC = [[MainViewController alloc] initWithTitle:@"通话记录"];

    PhotoViewController *photoVC = [[PhotoViewController alloc] initWithTitle:@"联系人"];

    LoginViewController *loginVC = [[LoginViewController alloc] initWithTitle:@"拨号"];

    SettingViewController *settingVC = [[SettingViewController alloc] initWithTitle: @"设置"];

    //设置子控制器集合
    tabController.viewControllers = @[mainVC, photoVC, loginVC, settingVC];

    self.window.rootViewController = tabController;

2、UITabBarController的简介和基本原理

#pragma mark - 设置标签栏按钮
    //图片
    UIImage *calllogImage = [UIImage imageNamed:@"calllog"];
    UIImage *contactImage = [UIImage imageNamed:@"contact@3x"];
    UIImage *dialImage    = [UIImage imageNamed:@"dial@3x"];
    UIImage *settingImage = [UIImage imageNamed:@"setting@3x"];

    //配置文本颜色
    tabController.tabBar.tintColor = [UIColor greenColor];

    //标签栏颜色
    tabController.tabBar.barTintColor = [UIColor colorWithRed:0.968 green:1.000 blue:0.964 alpha:1.000];
    //创建标签栏按钮
    UITabBarItem *contactItem  = [[UITabBarItem alloc] initWithTitle:@"通话记录" image:calllogImage tag:10];

    UITabBarItem *calllogItem  = [[UITabBarItem alloc] initWithTitle:@"联系人" image:contactImage tag:20];

    UITabBarItem *dialItem  = [[UITabBarItem alloc] initWithTitle:@"拨号" image:dialImage tag:30];

    UITabBarItem *settingItem = [[UITabBarItem alloc] initWithTitle:@"设置" image:settingImage tag:40];


    //对应各个控制器
    mainVC.tabBarItem = contactItem;
    mainVC.tabBarItem = calllogItem;
    mainVC.tabBarItem = dialItem;
    mainVC.tabBarItem = settingItem;

3、UITabBarController的层次结构

UITabBarController是选项卡栏导航控制器,显示效果是在页面底部有多个选项卡,通过点击不同选项卡可以在不同的ViewController之间进行切换。

这种对象的层次结构至少包含6个对象:
一个UITabBarController;
两个UIViewController;
一个UITabBar;
两个UITabBarItem;

UITabBarController是选项卡栏视图控制器,UITabBar是底部两个UITabBarItem的容器,管理两个UITabBarItem,每个UITabBarItem对应一个UIViewController,然后每个UIViewController都有自己的视图和视图控制器。

UITabBarController中有一个viewControllers属性,这是一个NSArray,包含选项卡控制器的视图控制器

下面来用代码创建一个UITabBarController:
下面是工程结构:

首先创建两个带xib文件的ViewController,分别为FirstViewController和SecondViewController
然后在AppDelegate.h中声明@property (strong, nonatomic) UITabBarController *tabBarController;,并添加协议UITabBarControllerDelegate

在.m中实现如下代码:

[cpp] view plaincopy 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
{  
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
    // Override point for customization after application launch.  

    //第一个tab的viewController  
    FirstViewController *firstViewController = [[FirstViewController alloc]init];  

    //如果在这里指定tabitem标题,则在FirstViewController中指定self.tabBarItem.title则不生效     
    firstViewController.title = @"First view";  

    UITabBarItem *firstItem = [[UITabBarItem alloc]initWithTitle:@"First" image:nil tag:1];  
    [firstItem setFinishedSelectedImage:[UIImage imageNamed:@"p1"] withFinishedUnselectedImage:[UIImage imageNamed:@"p1_f"]];  
    firstViewController.tabBarItem = firstItem;  


    SecondViewController *secondViewController = [[SecondViewController alloc]init];  

    //构建TabBarItem  
    UITabBarItem *secondItem = [[UITabBarItem alloc]initWithTitle:@"Second" image:nil tag:2];  

    //设置选中和非选中状态下的图片  
    [secondItem setFinishedSelectedImage:[UIImage imageNamed:@"p2_f"] withFinishedUnselectedImage:[UIImage imageNamed:@"p2"]];  
    //右上角小图标  
   [secondItem setBadgeValue:@"2"];  
    //指定tabBarItem  
    secondViewController.tabBarItem = secondItem;  
    [secondItem release];  

    //构建UITabBarController并指定代理为本身  
    self.tabBarController = [[[UITabBarController alloc]init] autorelease];  
    self.tabBarController.delegate = self;  

    //为UITabBarController添加TabBarItem  
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:firstViewController,secondViewController, nil];  

    [firstViewController release];  
    [secondViewController release];  

    //设置选中哪个tab  
//    [self.tabBarController setSelectedIndex:0];  

    //指定根视图  
    self.window.rootViewController  = self.tabBarController;  

    self.window.backgroundColor = [UIColor whiteColor];  
    [self.window makeKeyAndVisible];  
    return YES;  
}  

Coyp from :http://blog.sina.com.cn/s/blog_60a94ae701017bsb.html

4、如何通过UITabBarController推送到另一个控制器中(不同于模态视图推送)

    #pragma mark - delegate

    @interface AppDelegate () <UITabBarControllerDelegate>
    //设置协议遵守协议
    tabController.delegate = self;

    return YES;

#pragma mark - UITabBarControllerDelegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"%s",__func__);

}


- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSLog(@"%s",__func__);
    //获取单例
    UserInfo *userinfo = [UserInfo shareUserInfo];
    //当控制器界面为登录的时候才进行判断
    if (viewController == tabBarController.viewControllers[2]) {
        if (userinfo.isLogin) {
            return YES;
        } else {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"是否进行登录" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alertView show];
            return NO;
            }

        }
    return YES;
}

5、如果在UITabBarController管理的控制器之间进行传值操作

delegate,或者[[self viewControllers] objectAtIndex:i]可以取得相应的tab对应的UIViewController。
或者如果传递的数据不复杂的话,干脆用NSUserDefault来保存和读取。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值