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: