UITabBarController multi-view controller for IOS development (11)

Source: Internet
Author: User

1 Preface
UITabBarController is a multi-view controller. You can switch between different views. Let's take a look at its simple usage.


2. code example
ZYViewController. h:

 

[Plain]
# Import <UIKit/UIKit. h>
# Import "ZYFirstViewController. h"
# Import "ZYSecondViewController. h"
 
@ Interface ZYViewController: UIViewController
 
@ Property (nonatomic, strong) ZYFirstViewController * firstViewController; // The first view
 
@ Property (nonatomic, strong) ZYSecondViewController * secondViewController; // The second view
 
@ Property (nonatomic, strong) UITabBarController * taBarController; // multi-view Controller
 
@ Property (strong, nonatomic) UINavigationController * firstnavigationController; // The first view navigation bar
 
@ Property (strong, nonatomic) UINavigationController * secondnavigationController; // The second view navigation bar
 
@ End

# Import <UIKit/UIKit. h>
# Import "ZYFirstViewController. h"
# Import "ZYSecondViewController. h"

@ Interface ZYViewController: UIViewController

@ Property (nonatomic, strong) ZYFirstViewController * firstViewController; // The first view

@ Property (nonatomic, strong) ZYSecondViewController * secondViewController; // The second view

@ Property (nonatomic, strong) UITabBarController * taBarController; // multi-view Controller

@ Property (strong, nonatomic) UINavigationController * firstnavigationController; // The first view navigation bar

@ Property (strong, nonatomic) UINavigationController * secondnavigationController; // The second view navigation bar

@ 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]; // instantiate the view Array
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]; // instantiate the view Array
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) {// because the multi-view controller does not load non-homepage when loading, the initialization is put in this method.
Self. title = @ "First"; // set the title and the multi-view controller button is automatically assigned.
Self. tabBarItem. image = [UIImage imageNamed: @ "first.png"]; // Add an image
Self. view. backgroundColor = [UIColor whiteColor];
}
Return self;
}

-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil
{
Self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];
If (self) {// because the multi-view controller does not load non-homepage when loading, the initialization is put in this method.
Self. title = @ "First"; // set the title and the multi-view controller button is automatically assigned.
Self. tabBarItem. image = [UIImage imageNamed: @ "first.png"]; // Add an image
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;
}
Running result:

 

 
 

When you click Second:

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.