iOS开发之高级视图—— UITabBarController

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

       UITabBarController通常作为整个程序的rootViewController,而且不能添加到别的container viewController中。

    

       AppDelegate.m

      

//
//  AppDelegate.m
//  UITabBarControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    // 创建一个UITabBarController控制器
    ViewController* tabBarController = [[ViewController alloc] init];
    
    // 将UITabBarController 设置为window的根视图控制器
    self.window.rootViewController = tabBarController;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}



@end

    ViewController.h

//
//  ViewController.h
//  UITabBarControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UITabBarController<UITabBarControllerDelegate>

@end

    ViewController.m

//
//  ViewController.m
//  UITabBarControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "ViewController.h"
#import "AViewController.h"
#import "BViewController.h"
#import "CViewController.h"
#import "DViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 创建一个视图控制器
    AViewController *avc = [[AViewController alloc] init];
    // 创建一个UITabBarItem
    UITabBarItem *aItem = [[UITabBarItem alloc] initWithTitle:@"A" image:[UIImage imageNamed:@"bank.png"] tag:1];
    // 将创建的UITabBarItem设置为A视图控制器的TabBarItem
    [avc setTabBarItem:aItem];
    
    // B
    BViewController *bvc = [[BViewController alloc] init];
    UITabBarItem *bItem = [[UITabBarItem alloc] initWithTitle:@"B" image:[UIImage imageNamed:@"gear.png"] tag:2];
    bvc.tabBarItem = bItem;
    
    // C
    CViewController *cvc = [[CViewController alloc] init];
    UITabBarItem *cItem = [[UITabBarItem alloc] initWithTitle:@"C" image:[UIImage imageNamed:@"train.png"] tag:3];
    // 设置标记信息
    cItem.badgeValue = @"2";
    cvc.tabBarItem = cItem;
    
    // D
    DViewController *dvc = [[DViewController alloc] init];
    UITabBarItem *dItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:4];
    dvc.tabBarItem = dItem;
    
    self.viewControllers = [NSArray arrayWithObjects:avc,bvc,cvc,dvc, nil];
    
    // 默认从0开始
    self.selectedIndex = 1;
    
    // 当前显示的视图控制器
    UIViewController *selectVC = self.selectedViewController;
    NSLog(@"selectVC = [%@]",[selectVC class]);
    
    self.delegate = self;
    
}

#pragma mark - UITabBarControllerDelegate
/**
 当视图控制器被选择时激活
 @param tabBarController  选项卡视图控制器
 @param viewController  被选中的视图控制器
 */
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    NSLog(@"[%@]-----[%lu]",[viewController class],viewController.tabBarItem.tag);
    if (viewController.tabBarItem.tag == 3) {
        // 如果tabBarItem的标记不为空,设置标记为空
        if (viewController.tabBarItem.badgeValue != nil) {
            viewController.tabBarItem.badgeValue = nil;
        }
    }
}


@end

     AViewController.h

 

//
//  AViewController.h
//  UITabBarControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AViewController : UIViewController

@end

       AViewController.m

//
//  AViewController.m
//  UITabBarControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "AViewController.h"

@interface AViewController ()

@end

@implementation AViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

    BViewController.h

//
//  BViewController.h
//  UITabBarControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface BViewController : UIViewController

@end

   BViewController.m

//
//  BViewController.m
//  UITabBarControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "BViewController.h"

@interface BViewController ()

@end

@implementation BViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor greenColor];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

   CViewController.h

//
//  CViewController.h
//  UITabBarControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CViewController : UIViewController

@end

   CViewController.m

//
//  CViewController.m
//  UITabBarControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "CViewController.h"

@interface CViewController ()

@end

@implementation CViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor yellowColor];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

   DViewController.h

//
//  DViewController.h
//  UITabBarControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DViewController : UIViewController

@end

   DViewController.m

//
//  DViewController.m
//  UITabBarControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "DViewController.h"

@interface DViewController ()

@end

@implementation DViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

      效果图如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值