(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 bottom, 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) If we want to add or delete a view controller in the stack, we need to get the navigation controller, The general view controller in the stack has a self.navigationcontroller, which means that my navigation controller, which is the navigation controller where the view controller is located, gets the navigation controller.
(4) New view controller in the stack with Pushviewcontroller, in fact, push in one, 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 the user to open and jump pages. And pop is a lot of other ways to operate, such as suddenly pop out of just a root view controller, then it is equivalent to 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 has very many, 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 is displayed on the screen self.window.rootviewcontroller=nav1; 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 is also changed, because only display in the stack top view Controller view// So (1) control the so-called jump, in fact, the navigation controller in the control, inside the elements can be obtained through the Navigationcontroller property to their navigation controller//SO (2) after acquiring the navigation controller, the method of using push, 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 pop only can pop up the top of the stack, so can not specify the number of parameters// Poptorootviewcontrolleranimated-is to jump directly to the root view control chart, assuming that there are only two layers, then there is no difference with popviewcontrolleranimated, assuming that there are very many layers, So, in fact, it's the equivalent of not just pop out, but allAll view controllers outside the root view Control chart pop out, so it's equivalent to jumping to the root view controller//poptoviewcontroller-is to jump to the specified view controller xxx, this XXX must be in this stack inside, That must be in our current view controller of the following, so jump is to put themselves and in 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 general understanding is to initialize a view controller object with XXX yyy And 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, Just to get navigationcontroller, seems to be able to 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