Several proxy methods for iOS dev-30 Uitabbarcontroller and the order in which the view controller and view controller were selected when the Nsuserdefaults was last exited

Source: Internet
Author: User

One, several agent methods of Uitabbarcontroller


Add a protocol <uitabbarcontrollerdelegate> in the AppDelegate.h, and then appdelegate.m the following proxy method:

If there is no effect, the tbcon1.delegate=self may be missing; the proxy is assigned to the current appdelegate.

-(BOOL) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller    Shouldselectviewcontroller: (Uiviewcontroller *) viewcontroller{NSLog (@ "clicked"); return YES;} -(void) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller Didselectviewcontroller: (Uiviewcontroller *) viewcontroller{NSLog (@ "%@", Viewcontroller.title);} -(void) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller willbegincustomizingviewcontrollers: (NSArray *) viewcontrollers{NSLog (@ "would Customize");} -(void) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller didendcustomizingviewcontrollers: (NSArray *) Viewcontrollers changed: (BOOL) changed{if (changed) {NSLog (@ "changed!");}    else{NSLog (@ "not changed");    } for (Uiviewcontroller *vcs in viewcontrollers) {NSLog (@ "%@", vcs.title); }}-(void) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller didendcustomizingviewcontrollers: (NSArray *) Viewcontrollers changed: (BOOL) changed{} 


Second, the matters needing attention


(1) when calling title with the proxy method, it can only be called if the above view controller or navigation controller is assigned with a. Title method. Otherwise, the return value is null.


(2) The value assigned by the Initwithtitle method is assigned to. Tabbaritem.title and cannot be obtained directly with the. Title.


(3) So, if useful initwithtitle assignment, can be used again. Title is re-assigned again, which makes it easier to get the value in other methods.


Iii. using Nsuserdefaults to restore the last selected view


Create a nsuserdefaults in the Didselectviewcontroller proxy method to record the title of the selected view.

-(void) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller Didselectviewcontroller: (Uiviewcontroller *) viewcontroller{    //Select a view controller, the corresponding title is stored in the Nsuserdefaults, on the above use    //Create a nsuserdefaults, if the title can be taken, The title is assigned to store    nsuserdefaults *def1=[nsuserdefaults standarduserdefaults];    if (viewcontroller.title) {        [def1 setObject:viewController.title forkey:selectedkey];    }    Data synchronization to the application    [DEF1 synchronize];}


Then, add the following code to the Didfinishlaunchingwithoptions method, that is, if the last time you exit the program has a storage title, then look for the title corresponding to the view controller, and set this view controller as the default view controller for our open program, which is equivalent to restoring the last selected appearance. (Selectedkey is a macro variable of # define)

If there is storage in the nsuserdefaults, just take the value, and then compare with the existing label, you can know which label was last opened, and set the label as the default open    if ([[Nsuserdefaults Standarduserdefaults]objectforkey:selectedkey]) {for        (Uiviewcontroller *vcs1 in arr2) {            if ([Vcs1.title Isequaltostring:[[nsuserdefaults Standarduserdefaults]objectforkey:selectedkey]]) {                TBCON1.SELECTEDVIEWCONTROLLER=VCS1;}}}    

Iv. using Nsuserdefaults to restore the last view order


Create a nsuserdefaults in the Didendcustomizingviewcontrollers proxy method to record the order of the last view controller, and of course to record their title order.

-(void) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller didendcustomizingviewcontrollers: (NSArray *) Viewcontrollers changed: (BOOL) changed{    //Put the title of the new view controller after clicking done in order to exist in Def2, then use    nsuserdefaults *def2=[above Nsuserdefaults Standarduserdefaults];    Nsmutablearray *arr2=[[nsmutablearray Alloc]init];    For (Uiviewcontroller *vcs2 in viewcontrollers) {        [arr2 addObject:vcs2.title];    }    [Def2 setobject:arr2 Forkey:titleskey];    [Def2 synchronize];}

Then, add the following code to the Didfinishlaunchingwithoptions method, that is, if we capture the previously stored title array, start looking for its corresponding view controller from the first title and store the view controller in the new array, This way you can get a new sequential View controller array in order. Finally, assigning this new view controller array to the viewcontrollers of the tag controller is equivalent to restoring the View controller order when the program was last exited. (Titleskey is a macro variable for # define)

    Use the Order of the tab bar before closing the program    //Create an array to take out the array stored in the Nsuserdefaults    nsarray *newtitle1=[[nsuserdefaults Standarduserdefaults]objectforkey:titleskey];    If you do have this array, then iterate over the header array and the previous view controller array, and if the title of the View controller equals the first caption of the array, place the view controller inside a new array if    (newTitle1) {        Nsmutablearray *newarr1=[[nsmutablearray Alloc]init];        For (NSString *title1-newTitle1) {for            (Uiviewcontroller *vcs3 in arr2) {                if ([Vcs3.title isequaltostring:title 1]) {                    [newArr1 ADDOBJECT:VCS3];}}        }        Then assign the array of this new view controller to the tag controller        tbcon1.viewcontrollers=newarr1;    }

V. Read more about Nsuserdefaults


(1)nsuserdefaults precautions


(2) Nsuserdefaults Introduction and use


(3) Nsuserdefaults detailed

Several proxy methods for iOS dev-30 Uitabbarcontroller and the order in which the view controller and view controller were selected when the Nsuserdefaults restore was last exited

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.