你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> IOS開發之UITabBarController多視圖控制器

IOS開發之UITabBarController多視圖控制器

編輯:關於IOS

 

 

1 前言
UITabBarController為多視圖控制器,可以切換不同視圖,今天我們來學習一下其簡單用法。

2 代碼實例
ZYViewController.h:




#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:








@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:


- (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:


- (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;
}

運行結果:

 

IOS開發之UITabBarController多視圖控制器
當單擊Second時候:

 

 

IOS開發之UITabBarController多視圖控制器

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved