Introduction to view controller and view class in cocoa programming

Source: Internet
Author: User

Uitabbarcontroller class

The tab class allows users to move between multiple view controllers and customize the bar at the bottom of the screen. Provides a single click access to different views at the same time, providing the more button to the screen selected by the user and the screen of the edit bottom bar.

The convenience of the tab class is that you do not need to push and eject the view as a stack as the navigation bar does, but instead build a series of controllers (they can be uiviewcontroller, Uinavigationcontroller, Uitableviewcontroller or any other type of view controller) and add it to the tab bar by setting the Viewcontrollers property of the column so that each tab corresponds to an attempt controller.

First, create Uitabbarcontroller

Common is created in the applicationdidfinishlaunching: method of the application delegate class, which typically provides a 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 add to each specific tab

The code is as follows

Myviewcontroller *VC1 = [[Myviewcontroller alloc] init];

Myotherviewcontroller *VC2 = [[Myviewcontroller alloc] init];

Add these view controllers that are created first to an array object, and then assign this array to the Viewcontrollers property of the tab Bar Controller

The code is as follows

Nsarray *controllers = [Nsarray arraywithobjects:vc1,vc2,nil];

Tabbarcontrollers.viewcontrollers = controllers;

/* Also 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, creating a Uitabbarcontroller in the application delegate Appdelegate class creates a project based on the tab bar. However, depending on the situation, we can create a Uitabbarcontroller instance object directly in a separate view controller, such as customizing a Controller class Viewswitcherviewcontroller for view switching. You can create the required tabbarcontroller in the Viewdidload method.

Override the Init method in the view controller that implements the Uitabbarcontrollerdelegate delegate to customize the Uitabbaritem entry.

Code one is made by the Initwithnibname: method to load a specific view controller and customize the Tabbaritem style appearance of 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 two we can also rewrite the Init method directly within the 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 to allow Uitabbarcontroller to trigger these callback methods normally.

The code is as follows

Tabbarcontroller:didselectviewcontroller: is when the user selects a new tab, the Controller sends the message.

-(void) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller Didselectviewcontroller: (Uiviewcontroller *) Viewcontroller

{

Capture SelectedIndex to determine the currently selected tab

NSNumber *tabnumber = [NSNumber numberwithint:[tabbarcontroller SelectedIndex]];

Using the iphone's built-in user default system Nsuserdefaults, use Setobject:forkey: Set a value for the keyword

[[Nsuserdefaults Standarduserdefaults] setobject:tabnumber forkey:@ "Selectedtab"];

[[Nsuserdefaults standarduserdefaults] synchronize];

tab icon Item upper right corner red Little red circle digit hint

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

}

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.