"iOS Dev-21" Uinavigationcontroller Navigation controller initialization, navigation controller stack push and pop jump understanding

Source: Internet
Author: User

(1) When the navigation controller initialization usually has a root view controller, the navigation controller is equivalent to a stack, which is installed in the view controller, the first to go in the lowest side, the last to go in the top. At the top of the View Controller view is the interface that the navigation controller shows externally, that is, the user to see the interface.


(2) We need to load the navigation controller into the app, we need to set the navigation controller to the window's root view controller (all controller classes, can be assigned), which is equivalent to loading into the window.


(3) We want to add or delete a view controller in the stack, we need to get the navigation controller, Generally all the view controllers in the stack have a self.navigationcontroller, which means my navigation controller, which is the navigation controller where the view controller is located, so that I get the navigation controller.


(4) The new view controller in the stack with Pushviewcontroller, is actually push in a, so for the user is to open a new interface.


(5) Delete a view controller in the stack with popviewcontrolleranimated, of course, this pop can only pop the top one, for the user is equivalent from the current view to the top level view.


(6) In fact, this push and pop is an action for users to open and jump pages. And pop by more methods, such as suddenly pop off only a root view controller, then the equivalent from several layers directly back to the original main page. You can also specify a number of POPs to jump to the specified page.


(7) The most important should be this push and pop method, and pop there are many kinds, this understanding is not difficult to remember.


(a) in APPDELEGATE.M, add the following code:

#import "AppDelegate.h"//Because this class is required to instantiate an object, import #import "ViewController.h" @interface appdelegate () @ End@implementation appdelegate-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: ( Nsdictionary *) launchoptions {    //Create a view controller to be the root view controller of the navigation controller    Viewcontroller *root1=[[viewcontroller alloc] INIT];    Initialize the navigation controller to initialize the ROOT1 created above to it    Uinavigationcontroller *nav1=[[uinavigationcontroller alloc] INITWITHROOTVIEWCONTROLLER:ROOT1];    Finally, we set the window's root view controller as the navigation controller so that the navigation controller can display the SELF.WINDOW.ROOTVIEWCONTROLLER=NAV1 on the screen    ;    Override point for customization after application launch.    return YES;} @end

(b) Add the following code to VIEWCONTROLLER.M:

#import "ViewController.h"//Because an object needs to be instantiated, import it #import "SecondViewController.h" @interface Viewcontroller () @ End@implementation viewcontroller-(void) Viewdidload {//Create a button, click to enter child View controller, equivalent to enter sub-page UIButton *btn1=[uibutton button    Withtype:uibuttontyperoundedrect];    Btn1.frame=cgrectmake (38, 100, 300, 30);    [Btn1 settitle:@ "Jump to Secondviewcontroller" forstate:uicontrolstatenormal];    Btn1.backgroundcolor=[uicolor Whitecolor];    Self.view.backgroundcolor=[uicolor Redcolor];    [Btn1 addtarget:self Action: @selector (Jumpto) forcontrolevents:uicontroleventtouchupinside];    [Self.view ADDSUBVIEW:BTN1];    [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} -(void) jumpto{//The core of the two, the so-called jump, in fact, to the navigation controller stack push or pop a view controller, so the top of the view controller is changed, so the view also changed, because only display in the stack top view Controller view//SO ( 1) control so-called jump, in fact, is the navigation controller in the control, inside the elements can be obtained through the Navigationcontroller property to their navigation controller//SO (2) get to the navigation controller, using the Push method, Put a view controller into the stack senCon1, this new put in the top of the stack, it shows its view, so the user changed the page to jump SecondviewcontrOller *sencon1=[[secondviewcontroller Alloc]init]; [Self.navigationcontroller pushviewcontroller:sencon1 animated:yes];} @end

(c) Add the following code to the SECONDVIEWCONTROLLER.M:

#import "SecondViewController.h" @interface Secondviewcontroller () @end @implementation secondviewcontroller-(void)    viewdidload {UILabel *label1=[[uilabel alloc]init];    Label1.frame=cgrectmake (38, 80, 300, 30);    Label1.backgroundcolor=[uicolor Whitecolor];    [Email protected] "This is Secondviewcontroller";        [Self.view Addsubview:label1];    UIButton *btn2=[uibutton Buttonwithtype:uibuttontyperoundedrect];    Btn2.frame=cgrectmake (38, 120, 300, 30);    [Btn2 settitle:@ "Backto" forstate:uicontrolstatenormal];    Btn2.backgroundcolor=[uicolor Orangecolor];    [Self.view ADDSUBVIEW:BTN2];    [Btn2 addtarget:self Action: @selector (Backto) forcontrolevents:uicontroleventtouchupinside];    [Super Viewdidload]; Do any additional setup after loading the view.} Can manually set the pop out of the stack, equivalent to delete this page, jump to other pages//popviewcontrolleranimated is pop, because the pop-up can only pop up the top of the stack, so you can not specify parameters// Poptorootviewcontrolleranimated-is to jump directly to the root view control chart, if only two layers, then and popviewcontrolleranimated no difference, if there are many layers, So the equivalent of not only the pop out, but also all except the root view controlAll views outside of the mapping controller pop out, so it is equivalent to jump to the root view controller//poptoviewcontroller-is to jump to the specified view controller xxx, this XXX must be in this stack, that must be in our current view controller below the , so jump is to put themselves and in the xxx above all the view controller pop out, and then equivalent to jump directly to xxx//here is the focus of this XXX how to get, according to the general understanding is to use XXX to initialize a View controller object yyy, Then put this object yyy as the Poptoviewcontroller parameter//But the fact is, YYY is a new initialization, not in the stack, and of course, and in the stack of XXX initialization of the object is not the same object, so will be an error (because in the stack can not find AH)//So, Self.navigationController.viewControllers appearances, Viewcontrollers is the number of groups, stored when the navigation controller stack all the view controller, the first push into the 0, and so on, the top is definitely the last of the array So, the object that was initialized before XXX can be represented by [Self.navigationController.viewControllers objectatindex:0], where 0 is the root view controller//So, As long as get navigationcontroller, seemingly can do a lot of things-(void) backto{[Self.navigationcontroller poptoviewcontroller:[ Self.navigationController.viewControllers objectatindex:0] animated:yes];} @end

Cut a Picture:


"iOS Dev-21" Uinavigationcontroller Navigation controller initialization, navigation controller stack push and pop jump understanding

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.