IOS開發(11)之UITabBarController多視圖控制器

來源:互聯網
上載者:User

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時候:

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.