Define yourself Uitabbarcontroller

Source: Internet
Author: User

Most of the online definition of Tabbar are inherited view, the project to use path+tabbat such words (path with the Mmdrawercontroller this framework), inherit the view Tabbar is unable to meet the conditions (not a container). The following is an introduction to the self-written definition of Tabbar.

And the system comes in almost the same way, first look at the original wording such as the following:

First define a uitabbarcontroller* tabbarviewcontroller in. h; And then in. m to do it,

Tabbarviewcontroller = [[Uitabbarcontroller alloc]init];    firstviewcontroller* first = [[Firstviewcontroller alloc]init];    Uinavigationcontroller * Navone = [[Uinavigationcontroller alloc] initwithrootviewcontroller:first];       [Navone Setnavigationbarhidden:true];    secondviewcontroller* second = [[Secondviewcontroller alloc]init];    Uinavigationcontroller * Navtwo = [[Uinavigationcontroller alloc] initwithrootviewcontroller:second];        [Navtwo Setnavigationbarhidden:true];    ViewController3 *VC3 = [[ViewController3 alloc] init];    Uinavigationcontroller * nav3 = [[Uinavigationcontroller alloc] initwithrootviewcontroller:vc3];    [Nav3 Setnavigationbarhidden:true];    ViewController5 *vc5 = [[ViewController5 alloc] init];    Uinavigationcontroller * nav5 = [[Uinavigationcontroller alloc] initwithrootviewcontroller:vc5];        [Nav5 Setnavigationbarhidden:true]; Tabbarviewcontroller.viewcontrollers = [Nsarray arraywithobjects:navone, navtwo,nav3,nav5, nil];    [First release];    [Second release];    [VC3 release];        [VC5 release];    Uitabbar *tabbar = Tabbarviewcontroller.tabbar;    Uitabbaritem *tabbaritem1 = [Tabbar.items objectatindex:0];    Uitabbaritem *tabbaritem2 = [Tabbar.items objectatindex:1];    Uitabbaritem *tabbaritem3 = [Tabbar.items objectatindex:2];        Uitabbaritem *tabbaritem4 = [Tabbar.items objectatindex:3];    Tabbaritem1.title = @ "message";    Tabbaritem2.title = @ "Application";    Tabbaritem3.title = @ "Contacts";        Tabbaritem4.title = @ "I"; [TabBarItem1 setfinishedselectedimage:[uiimage imagenamed:@ "Home_selected.png"] withfinishedunselectedimage:[    UIImage imagenamed:@ "Home.png"]; [TabBarItem2 setfinishedselectedimage:[uiimage imagenamed:@ "Maps_selected.png"] withfinishedunselectedimage:[    UIImage imagenamed:@ "Maps.png"]; [TabBarItem3 setfinishedselectedimage:[uiimage imagenamed:@ "Myplan_selected.png"] withfinishedunselectedimage:[    UIImage imagenamed:@ "Myplan.png"]; [TabBarItem4 Setfinishedselectedimage:[uiiMage imagenamed:@ "Settings_selected.png"] withfinishedunselectedimage:[uiimage imagenamed:@ "Settings.png"];    uiimage* tabbarbackground = [UIImage imagenamed:@ "Tabbar.png"];   [[Uitabbar appearance] setbackgroundimage:tabbarbackground];        [[Uitabbar appearance] Setselectionindicatorimage:[uiimage imagenamed:@ "Tabbar_selected.png"];                                                       [[Uitabbaritem appearance] Settitletextattributes:[nsdictionary Dictionarywithobjectsandkeys:                                                       [Uicolor Whitecolor], Uitextattributetextcolor,   NIL] forstate:uicontrolstatenormal];   Uicolor *titlehighlightedcolor = [Uicolor Greencolor];                                                       [[Uitabbaritem appearance] Settitletextattributes:[nsdictionary Dictionarywithobjectsandkeys:                                                       Titlehighlightedcolor, Uitextattributetextcolor, NIL] forstate:uicontrolstateselected];


Here's a look at your own definition of the method, also defined in the. h file

Uitabbarcontroller* tabbarviewcontroller;

In defining a long preselect variable, this variable is used to infer which selectedindex the click is.

The detailed implementations in. m are as follows:

 Nsmutablearray *arrcontroller = [[Nsmutablearray alloc]init]; Information Messageviewcontroller *MESSAGEVC = [[Messageviewcontroller alloc]initwithnibname:@ "MessageViewController"        Bundle:nil];    Uinavigationcontroller * Navmessage = [[Uinavigationcontroller alloc] INITWITHROOTVIEWCONTROLLER:MESSAGEVC];        [MESSAGEVC release];    [Navmessage Setnavigationbarhidden:true];    [Arrcontroller Addobject:navmessage];    [Navmessage release];        Application Appsviewcontroller *appsvc = [[Appsviewcontroller alloc]initwithnibname:@ "Appsviewcontroller" bundle:nil];    Uinavigationcontroller *navapps = [[Uinavigationcontroller alloc] initwithrootviewcontroller:appsvc];    [Appsvc release];    [Navapps Setnavigationbarhidden:true];    [Arrcontroller Addobject:navapps];    [Navapps release]; Contacts Addressviewcontroller *addressvc = [[Addressviewcontroller alloc]initwithnibname:@ "AddressViewController"        Bundle:nil]; Uinavigationcontroller *navaddress = [[Uinavigationcontroller ALloc] initwithrootviewcontroller:addressvc];    [Addressvc release];    [Navaddress Setnavigationbarhidden:true];    [Arrcontroller addobject:navaddress];    [Navaddress release];        I meviewcontroller *MEVC = [[Meviewcontroller alloc]initwithnibname:@ "Meviewcontroller" bundle:nil];    Uinavigationcontroller *navme = [[Uinavigationcontroller alloc] INITWITHROOTVIEWCONTROLLER:MEVC];    [MEVC release];    [Navme Setnavigationbarhidden:true];    [Arrcontroller Addobject:navme];            [Navme release];        Tabbarviewcontroller=[[uitabbarcontroller alloc] init];    Tabbarviewcontroller.viewcontrollers=arrcontroller;    [Arrcontroller release];    Preselect = 1;        for (int i = 0; i<4; i++) {nsstring *nalstring = [NSString stringwithformat:@ "Tabbar_unselecte%d.png", i+1];        NSString *highstring = [NSString stringwithformat:@ "Tabbar_selected%d.png", i+1];        UIImage *nalimage = [UIImage imagenamed:nalstring]; UIImage *highimage = [UIImage imagenamed:Highstring];        UIButton *tabbarbtn = [UIButton buttonwithtype:uibuttontypecustom];        Tabbarbtn.frame = CGRectMake (i* (320/4), 0, (320/4), 46);        [Tabbarbtn addtarget:self Action: @selector (tabbarbtnpress:) forcontrolevents:uicontroleventtouchupinside];        [Tabbarbtn setbackgroundimage:nalimage Forstate:uicontrolstatenormal];        [Tabbarbtn setbackgroundimage:highimage forstate:uicontrolstateselected];        Tabbarbtn.tag = 100+i;                [Tabbarviewcontroller.tabbar ADDSUBVIEW:TABBARBTN];            if (i = = 1) {tabbarbtn.selected = YES;        Tabbarviewcontroller.selectedindex=1; }    }

The main idea is to define Uitabbarcontroller's Tabbar property, which is addsubview on top of it. Because of the UIButton, now it is necessary to achieve the click

method to switch.

Methods such as the following:

-(void) Tabbarbtnpress: (UIButton *) tabbarbtn{    if (preselect! = tabbarbtn.tag-100) {                UIButton *prvbtn = ( UIButton *) [Tabbarbtn.superview viewwithtag:preselect+100];        prvbtn.selected = NO;                Tabbarviewcontroller.selectedindex = Preselect = tabbarbtn.tag-100;                UIButton *button = (UIButton *) [Tabbarbtn.superview ViewWithTag:tabbarBtn.tag];        button.selected = YES;}    }

That's it. There is something wrong with the place please point out to us.

5.29 Day Supplement:

It says that by default, the 2nd Tabbar is selected, and the first one is selected. Only need to change preselect = 0;

if (i==0)

{

tabbarbtn. selected = YES ;

self. SelectedIndex = 0 ;

}

Assuming that you want to separate this, you just need to define a class that inherits Uitabbarcontroller.

Suppose you want to use your own defined background as well. The ability to hide the system (self. TabBar. Hidden =YES;), define a view and add it to the Tabbar. [Self.view addsubview:xxxx]; , others use the method above.



6.3:

Found a problem. VC NAV does not show, find the next reason, found Tabbarcontroller. viewcontrollers the set in is a VC array, not a nav array.



Define yourself Uitabbarcontroller

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.