Uitabbarcontroller label bar controller-iOS development

Source: Internet
Author: User
Statement

You are welcome to repost this article, but please respect the author's Labor achievements. repost this article and keep the statement in this box. Thank you.
Article Source: http://blog.csdn.net/iukey

In uikit, uitabbar represents the label bar, And uitabbarcontroller encapsulates it, making it easier to manage and switch multiple views.

To create a tag bar controller, you must first prepare a separate page for each button. Each page should be created as a uiviewcontroller object.

Construct a controller array:

Your application may have multiple controllers trying to implement different functions. If you are writing a music player, there may be some controllers, such as musiclist, currentplay, favourite, singerlist, and settings. Before creating your tab bar, you should first create an array and place the View Controller object you want to display in the tab bar.

// Generate the musiclist * musiclist = [[[musiclist alloc] init] autorelease]; currentplay * currentplay = [[currentplay alloc] init] autorelease]; favourite * favorite = [[favourite alloc] init] autorelease]; singerlist * singerlist = [[[singerlist alloc] init] autorelease]; settings * settings = [[settings alloc] initwithstyle: Custom] autorelease]; // Add an array nsarray * controllerarray = [[nsarray alloc] initwithobjects: musiclist, currentplay, favorite, singerlist, settings, nil];

Configure button properties:

Each tag bar has its own "tag", which defines what the tag button looks like. In the init method of the View Controller, you can configure the tag bar button to define the title and/or tabbaritem attributes of the View:

- (id)initWithStyle:(UITableViewStyle)style{    self = [super initWithStyle:style];    if (self) {    self.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"Settings" image:[UIImage imageNamed:@"Setting"] tag:4];    }    return self;}

Set the tabbaritem attribute to a uitabbaritem object. You can initialize the project in the label bar in two ways. One is initwithtitle, which allows you to customize titles, images, and other data to display buttons. The other is the button provided by the creation system. The latter is as follows:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {      self.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];    }    return self;}

The buttons provided by the system are as follows:

typedef enum {    UITabBarSystemItemMore,    UITabBarSystemItemFavorites,    UITabBarSystemItemFeatured,    UITabBarSystemItemTopRated,    UITabBarSystemItemRecents,    UITabBarSystemItemContacts,    UITabBarSystemItemHistory,    UITabBarSystemItemBookmarks,    UITabBarSystemItemSearch,    UITabBarSystemItemDownloads,    UITabBarSystemItemMostRecent,    UITabBarSystemItemMostViewed,} UITabBarSystemItem;

Display the label bar controller:

All controllers required in the tag bar are ready. Now we can generate our tag bar controller. I forgot to talk about it. The controller is

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) generated in launchoptions.

// Create uitabbarcontroller controller uitabbarcontroller * tabbarcontroller = [[uitabbarcontroller alloc] init]; // set the viewcontrollers attribute of the uitabbarcontroller controller to the previously generated array controllerarray tabbarcontroller. viewcontrollers = controllerarray; // The default value is 2nd view tabs (index starts from 0) tabbarcontroller. selectedindex = 1; // Add the view of tabbarcontroller to window [self. window addsubview: tabbarcontroller. view];

Customizable buttons
By default, when there are more than five buttons, the label bar controller allows you to customize the button layout. To do this, click the tag "more" and then click the edit button in the navigation bar that appears. You can choose to customize only some specific labels, or you can completely disable customization. To allow customization, set customizableviewcontrollers of the Controller in the label bar to an array containing the attempt controller you want to customize:

Navigation

When the tab bar controller is displayed, the Controller automatically switches the view corresponding to the selected tab to the front-end of the screen. To read or change the attempt controller of the current activity, you can use the selectedviewcontroller attribute:

Tabbarcontroller. selectedviewcontroller = musiclist; // read uiviewcontroller * activecontroller = tabbarcontroller. selectedviewcontroller; If (activecontroller = musiclist ){//}

You can also use indexes:

tabBarController.selectedIndex = 1;

Delegated proxy

To be notified when the view on the tab bar is selected, assign a delegate to the tab bar controller:

tabBarController.delegate = self;

The delegate will be notified when a tab is selected, and then the delegate method of didselectviewcontroller will be called:

-(Void) tabbarcontroller :( uitabbarcontroller *) tabbarcontroller didselectviewcontroller :( uiviewcontroller *) viewcontroller {/* Add code to process operations after the custom tab bar ends */}

Now, the Code project file is attached.
Uitabbarviewcontrollerdemo

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.