UITabBarController是IOS中很常用的一个viewController,UITabBarController是选项卡栏导航控制器,显示效果是在页面底部有多个选项卡,通过点击不同选项卡可以在不同的ViewController之间进行切换。
AViewController.h
BViewController.h
BViewController.m
CViewController.h
CViewController.m
DViewController.h
DViewController.m
效果图如下:
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
效果图如下: