1 前言
UITabBarController為多視圖控制器,可以切換不同視圖,今天我們來學習一下其簡單用法。
2 代碼實例
ZYViewController.h:
[plain]
#import <UIKit/UIKit.h>
#import "ZYFirstViewController.h"
#import "ZYSecondViewController.h"
@interface ZYViewController : UIViewController
@property(nonatomic,strong) ZYFirstViewController *firstViewController;//第一個視圖
@property(nonatomic,strong) ZYSecondViewController *secondViewController;//第二個視圖
@property(nonatomic,strong) UITabBarController *taBarController;//多視圖控制器
@property (strong,nonatomic) UINavigationController *firstnavigationController;//第一個視圖導航欄
@property (strong,nonatomic) UINavigationController *secondnavigationController;//第二個視圖導航欄
@end
#import <UIKit/UIKit.h>
#import "ZYFirstViewController.h"
#import "ZYSecondViewController.h"
@interface ZYViewController : UIViewController
@property(nonatomic,strong) ZYFirstViewController *firstViewController;//第一個視圖
@property(nonatomic,strong) ZYSecondViewController *secondViewController;//第二個視圖
@property(nonatomic,strong) UITabBarController *taBarController;//多視圖控制器
@property (strong,nonatomic) UINavigationController *firstnavigationController;//第一個視圖導航欄
@property (strong,nonatomic) UINavigationController *secondnavigationController;//第二個視圖導航欄
@endZYViewController.m:
[plain]
@synthesize firstViewController;
@synthesize secondViewController;
@synthesize taBarController;
@synthesize firstnavigationController;
@synthesize secondnavigationController;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"UITabBarControllerTest";
self.firstViewController = [[ZYFirstViewController alloc] initWithNibName:nil bundle:nil];
firstnavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
self.secondViewController = [[ZYSecondViewController alloc] initWithNibName:nil bundle:nil];
secondnavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
NSArray *twoViewControllers = [[NSArray alloc] initWithObjects:firstnavigationController,secondnavigationController, nil];//實例化視圖數組
self.taBarController = [[UITabBarController alloc] init];
[self.taBarController setViewControllers:twoViewControllers];
[self.view addSubview:taBarController.view];
}
@synthesize firstViewController;
@synthesize secondViewController;
@synthesize taBarController;
@synthesize firstnavigationController;
@synthesize secondnavigationController;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"UITabBarControllerTest";
self.firstViewController = [[ZYFirstViewController alloc] initWithNibName:nil bundle:nil];
firstnavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
self.secondViewController = [[ZYSecondViewController alloc] initWithNibName:nil bundle:nil];
secondnavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
NSArray *twoViewControllers = [[NSArray alloc] initWithObjects:firstnavigationController,secondnavigationController, nil];//實例化視圖數組
self.taBarController = [[UITabBarController alloc] init];
[self.taBarController setViewControllers:twoViewControllers];
[self.view addSubview:taBarController.view];
}
ZYFirstViewController.m:
[plain]
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {//由於多視圖控制器加載時候不會加載非首頁面,所以把初始化放在該方法中
self.title = @"First";//設置標題,並會自動賦給多視圖控制器按鈕
self.tabBarItem.image = [UIImage imageNamed:@"first.png"];//添加圖片
self.view.backgroundColor = [UIColor whiteColor];
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {//由於多視圖控制器加載時候不會加載非首頁面,所以把初始化放在該方法中
self.title = @"First";//設置標題,並會自動賦給多視圖控制器按鈕
self.tabBarItem.image = [UIImage imageNamed:@"first.png"];//添加圖片
self.view.backgroundColor = [UIColor whiteColor];
}
return self;
}
ZYSecondViewController.m:
[plain]
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.title = @"Second";
self.tabBarItem.image = [UIImage imageNamed:@"second.png"];
self.view.backgroundColor = [UIColor whiteColor];
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.title = @"Second";
self.tabBarItem.image = [UIImage imageNamed:@"second.png"];
self.view.backgroundColor = [UIColor whiteColor];
}
return self;
}
運行結果:
當單擊Second時候: