Using Uitabbarcontroller to build the main interface of the project

Source: Internet
Author: User

A: Brief introduction of Uitabbarcontroller:

1. Steps to use:

(1) Initialize Uitabbarcontroller

(2) Set UIWindow Rootviewcontroller to Uitabbarcontroller

(3) Create the corresponding sub-controller (Viewcontroller) (with the child controller as the root view controller of the navigation controller, the outermost wrapper a layer of navigation bar)

(4) Add the controller to the Uitabbarcontroller

2: Join mode: (1)[TB ADDCHILDVIEWCONTROLLER:C1];

(2)[email protected][c1,c2,c3,c4];

Note: The order of presentation is the same as the order of addition, unlike the navigation controller, which is displayed in front of the view that corresponds to the first added controller. The navigation controller is the process of pressing the stack

3.UITabBar

The toolbar below is called Uitabbar, and if the Uitabbarcontroller has n sub-controllers, there will be N Uitabbarbutton as child controls within the Uitabbar.

Note: The position of Uitabbarbutton in Uitabbar is evenly divided, and the height of Uitabbar is 49.

In the above program, Uitabbarcontroller has 4 sub-controllers, so there are 4 uitabbarbutton,uitabbar structures in the Uitabbar, as shown here:

2.UITabBarButton

Uitabbarbutton? What content is determined by the Tabbaritem attribute of the corresponding sub-controller

c1.tabbaritem.title=@ " message "; C1.tabbaritem.image=[uiimage imagenamed:@ "tab_recent_nor"];      

3: Detailed Reference blog: http://www.cnblogs.com/wendingding/p/3775488.html

Second: The application of Uitabbarcontroller in the project

1: Set the window's root view controller to Cztabbarcontroller

2:

#import<UIKit/UIKit.h>@interfaceCztabbarcontroller:uitabbarcontroller@end#import "CZTabBarController.h"#import "uiimage+image.h"#import "CZTabBar.h"#import "CZHomeViewController.h"#import "CZMessageViewController.h"#import "CZDiscoverViewController.h"#import "CZProfileViewController.h"#import "CZNavigationController.h"#import "CZUserTool.h"#import "CZUserResult.h"#import "CZComposeViewController.h"@interfaceCztabbarcontroller () <CZTabBarDelegate>@property (nonatomic, strong) Nsmutablearray*items; @property (nonatomic, weak) Czhomeviewcontroller*home; @property (nonatomic, weak) Czmessageviewcontroller*message; @property (nonatomic, weak) Czprofileviewcontroller*Profile ;@end@implementationCztabbarcontroller#pragmaMark-Lazy loading the array that holds the Tabbaritem-(Nsmutablearray *) items{if(_items = =Nil) {_items=[Nsmutablearray array]; }    return_items;}- (void) viewdidload {[Super viewdidload]; //1: Add all sub-controllers[self setupallchildviewcontroller]; //2: Custom Tabbar[self setuptabbar]; //3: No readings are requested at every time[Nstimer Scheduledtimerwithtimeinterval:2target:self selector: @selector (requestunread) Userinfo:nil repeats:yes];}//Request not reading- (void) requestunread{//unread request for Weibo[Czusertool unreadwithsuccess:^ (Czuserresult *result) {        //Set Home Page not reading_home.tabbaritem.badgevalue = [NSString stringWithFormat:@"%d", Result.status]; //set Message not reading_message.tabbaritem.badgevalue = [NSString stringWithFormat:@"%d", Result.messagecount]; //set my non-readings_profile.tabbaritem.badgevalue = [NSString stringWithFormat:@"%d", Result.follower]; //set all the unread of the application[UIApplication Sharedapplication].applicationiconbadgenumber =Result.totoalcount; } Failure:^ (Nserror *error) {            }];}#pragmaMark-Set the custom tabbar-(void) setuptabbar{/*1:self.tabbar The controller's Tabbar, to add a custom tabbar, you need to remove the tabbar that comes with your system **/            //Custom TabbarCztabbar *tabbar =[[Cztabbar alloc] initWithFrame:self.tabBar.frame]; Tabbar.backgroundcolor=[Uicolor Whitecolor]; //Set up proxyTabBar.Delegate=Self ; //transfer Tabbaritem model to TabbarTabbar.items =Self.items; //Add custom Tabbar[Self.view Addsubview:tabbar]; //removing the Tabbar from the system[Self.tabbar Removefromsuperview];}#pragmaMark-When you click on the button on the Tabbar call and switch the view-(void) TabBar: (Cztabbar *) TabBar Didclickbutton: (nsinteger) index{/*1:self.selectedindex = index; This line code is important, and executing this code switches the view*/        if(Index = =0&& Self.selectedindex = = index) {//Click Home, Refresh[_home Refresh]; } Self.selectedindex=index;}#pragmaMark-Called when the plus button is clicked-(void) Tabbardidclickplusbutton: (Cztabbar *) tabbar{/*1:presentviewcontroller, if you want the view to have a navigation bar, let it be a root view controller for the navigation controller*/    //Create a Send micro-bo controllerCzcomposeviewcontroller *COMPOSEVC =[[Czcomposeviewcontroller alloc] init]; Cznavigationcontroller*nav =[[Cznavigationcontroller alloc] INITWITHROOTVIEWCONTROLLER:COMPOSEVC]; [Self Presentviewcontroller:nav animated:yes completion:nil];}#pragmaMark-Add all the sub-controllers-(void) setupallchildviewcontroller{/*Step 1: Create an object for all the sub-controllers and wrap a navigation controller on the outer layer of each controller 2: Set the Tabbaritem of each sub-controller to select the unselected picture, and the header 2 for each sub-controller: Because the process repeats the same code, pumping The code is encapsulated into a method for external access, and the parameters provided include: Image,selectedimage,title*/        //Home PageCzhomeviewcontroller *home =[[Czhomeviewcontroller alloc] init]; [Self setuponechildviewcontroller:home image:[uiimage imagenamed:@"Tabbar_home"] Selectedimage:[uiimage Imagewithoriginalname:@"tabbar_home_selected"] Title:@"Home Page"]; _home=home; //messageCzmessageviewcontroller *message =[[Czmessageviewcontroller alloc] init]; [Self setuponechildviewcontroller:message image:[uiimage imagenamed:@"Tabbar_message_center"] Selectedimage:[uiimage Imagewithoriginalname:@"tabbar_message_center_selected"] Title:@"message"]; _message=message; //foundCzdiscoverviewcontroller *discover =[[Czdiscoverviewcontroller alloc] init]; [Self setuponechildviewcontroller:discover image:[uiimage imagenamed:@"Tabbar_discover"] Selectedimage:[uiimage Imagewithoriginalname:@"tabbar_discover_selected"] Title:@"found"]; //I'mCzprofileviewcontroller *profile =[[Czprofileviewcontroller alloc] init]; [Self setuponechildviewcontroller:profile image:[uiimage imagenamed:@"Tabbar_profile"] Selectedimage:[uiimage Imagewithoriginalname:@"tabbar_profile_selected"] Title:@"I'm"]; _profile=Profile ;}#pragmaMark-Add each sub controller-(void) Setuponechildviewcontroller: (Uiviewcontroller *) VC Image: (UIImage *) image selectedimage: (UIImage *) selectedimage Title: (NSString *) title{//1: Set the title of each sub-controller (the title of the sub-controller and the Tabbar header only need to be set once Navigationitem determines the content on the navigation bar is determined by the navigationitem of the stack top controllerVc.title =title; //2: Set each word controller TabbaritemVc.tabBarItem.image =image; Vc.tabBarItem.selectedImage=SelectedImage; //3: Save Tabbaritem model to array[Self.items AddObject:vc.tabBarItem]; //4: Use the child controller as the root view controller for the navigation controllerCznavigationcontroller *nav =[[Cznavigationcontroller alloc] INITWITHROOTVIEWCONTROLLER:VC]; //5: Add a sub-controller[self Addchildviewcontroller:nav];}@end

Using Uitabbarcontroller to build the main interface of the project

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.