Introduction to visual controllers and views in CoCoa Programming

Source: Internet
Author: User

UITabBarController class

The option card class allows you to move between multiple view controllers and customize this column at the bottom of the screen. Provide one-click access to different views, and provide the More button to the screen selected by the user and the screen in the bottom bar of the editing.

The convenience of the option card class is that you do not need to push and pop up the view in stack mode like in the navigation bar, instead, they form a series of controllers (they can be UIViewController, UINavigationController, UITableViewController, or any other type of View Controller) and add them to the tab bar by setting the viewControllers attribute of the bar, make each tab correspond to an attempt controller.

1. Create UITabBarController

It is common to create in the applicationDidFinishLaunching: Method of the application delegate class, which usually provides the root view for the application window.

// Create a UITabBarController object

The Code is as follows:

UITabBarController * tabBarController = [[UITabBarController alloc] init];

// TabBarController. delegate = self;

// Create a series of view controllers to be added to each specific Tab card

The Code is as follows:

MyViewController * vc1 = [[MyViewController alloc] init];

Mytherviewcontroller * vc2 = [[MyViewController alloc] init];

// Add the created view controllers to an Array object, and then assign the Array to the viewControllers attribute of the Tab Bar Controller.

The Code is as follows:

NSArray * controllers = [NSArray arrayWithObjects: vc1, vc2, nil];

TabBarControllers. viewControllers = controllers;

/* Or

NSMutableArray * controllers = [[NSMutableArray alloc] initWithCapacity: 2];

[Controllers addObject: vc1];

[Controllers addObject: vc2];

*/

// Add the current view of the TabBar controller to the window

[Window addSubview: tabBarController. view];

Of course, to create a UITabBarController in the application delegate AppDelegate class is equivalent to creating a project based on the Tab Bar. However, you can also create a UITabBarController instance object directly in an independent view controller, such as a custom ViewSwitcherViewController for view switching, you can create the required TabBarController in the viewDidLoad method.

In the View Controller that implements the UITabBarControllerDelegate delegate, rewrite the init method to customize UITabBarItem entries.

Code 1: The initWithNibName method is used to load a specific view controller and customize the TabBarItem style appearance in the controller.

The Code is as follows:

-(Id) init {

If (self = [super initWithNibName: @ "MyViewController" bundle: nil]) {

Self. title = @ "My View Controller ";

UIImage * anImage = [UIImage imageNamed: @ "MyImage.png"];

UITabBarItem * theItem = [[UITabBarItem alloc] initWithTitle: @ "Home" image: anImage tag: 0];

Self. tabBarItem = theItem;

[TheItem release];

}

Return self;

}

Code 2: You can directly rewrite the init method in a specific view controller.

The Code is as follows:

-(Id) init {

If ([super init]! = Nil ){

UITabBarItem * item = [[UITabBarItem alloc] initWithTitle: @ "Home" image: [UIImage imageNamed: @ "MyImage.png"] tag: 0];

Self. tabBarItem = item;

[Item release];

}

Return self;

}

Implement the required delegate methods so that UITabBarController can trigger these callback methods normally.

The Code is as follows:

TabBarController: didSelectViewController: This message is sent when you select a new tab.

-(Void) tabBarController :( UITabBarController *) tabBarController didSelectViewController :( UIViewController *) viewController

{

// Capture selectedIndex to determine the Selected tab

NSNumber * tabNumber = [NSNumber numberWithInt: [tabBarController selectedIndex];

// Use the default NSUserDefaults System of the iPhone built-in user, and use setObject: forKey: to set the value for the keyword

[[NSUserDefaults standardUserDefaults] setObject: tabNumber forKey: @ "selectedTab"];

[[NSUserDefaults standardUserDefaults] synchronize];

// A red circle number is displayed in the upper-right corner of the tab icon item.

ViewController. tabBarItem. badgeValue = [NSString stringWithFormat: @ "% d", 80];

}

Related Article

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.