[BS-20] The difference between the navigation controller and the view controller in adding controllers

Source: Internet
Author: User

The difference between the navigation controller and the view controller in adding controllers

1.

Because the navigation controller has a navigation stack, there is an array viewcontrollers that the normal view controller does not have, and the view controller that joins the array enters the navigation stack by default in push mode. The navigation controller has a [Uinavigationcontroller alloc] INITWITHROOTVIEWCONTROLLER:ROOTVC]; Initialization method, call this method is equivalent to add ROOTVC to the first position of the array viewcontrollers, as the bottom of the stack ROOTVC is not going to be out of the pop, and the navigation controller together life and death. VC added to the navigation stack, when pushed to the top of the stack, Vc.view will be created/loaded, and displayed on the window, not the top of the stack Vc.view added to the next layer of Vc.view, the next layer of Vc.view is not in the UI level.

2.

All view controllers have a method of [Viewcontroller ADDCHILDVIEWCONTROLLERL:VC] that uses this method to join the VC, is automatically stored in the childviewcontrollers array of Viewcontroller (similar to the UIView subviews array).

Note: You can only add elements to the childviewcontrollers array by using the Addchildviewcontroller method, and you cannot assign them directly because the childviewcontrollers array is read-only to the programmer.

Also: all VCs in Viewcontroller.childviewcontrollers only represent Viewcontroller sub-controllers. The view of the sub-controller is not automatically loaded into the Viewcontroller.view, and the child controller view needs to be manually added to the parent View Controller view or any child view of the view to display on the screen.

3.

Because the navigation controller belongs to the view controller, the navigation controller can also pass [NAVC ADDCHILDVIEWCONTROLLER:VC]; Add a child view controller to your childviewcontrollers array, but the VC added by this method is automatically added to the navigation stack array viewcontrollers. When a VC appears at the top of the stack, its view is automatically loaded.

So for the navigation controller viewcontrollers and childviewcontrollers content is identical, the difference is that childviewcontrollers is read-only, The viewcontrollers is a Nsarray immutable group, which can be assigned an initial value.

4.

Note the difference:

Self. Childviewcontrollers,

Self. Presentedviewcontroller,

Self. Presentingviewcontroller,

Self. Presentingviewcontroller.childviewcontrollers,

Self. Navigationcontroller.viewcontrollers,

Self. Navigationcontroller.childviewcontrollers,

Self. navigationcontroller.viewcontrollers[0].childviewcontrollers

5.

Three ways to initialize Navigationcontroller:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {_window=[[UIWindow Alloc]initwithframe:[[uiscreen Mainscreen]bounds]]; _window.backgroundcolor=[Uicolor Whitecolor]; Wzessenceviewcontroller*ESSENCEVC = [WzessenceviewcontrollerNew]; Wznewviewcontroller*NEWVC = [WznewviewcontrollerNew]; Wzfriendviewcontroller*FRIENDVC = [WzfriendviewcontrollerNew]; Wzmeviewcontroller*MEVC = [WzmeviewcontrollerNew]; //Method One//Uinavigationcontroller *nav = [[Uinavigationcontroller ALLOC]INITWITHROOTVIEWCONTROLLER:ESSENCEVC];//[Essencevc.navigationcontroller PUSHVIEWCONTROLLER:NEWVC Animated:yes]; //Method TwoUinavigationcontroller *nav =[[Uinavigationcontroller alloc]init]; Nav.viewcontrollers= @[essencevc, NEWVC, FRIENDVC, MEVC];//enter the navigation station by default in push mode//nav.childviewcontrollers = @[essencevc, NEWVC, FRIENDVC, MEVC];//is read-only and cannot be assigned a valueNSLog (@"nav.viewcontrollers:%@ \ nav.childviewcontrollers:%@", nav.viewcontrollers,nav.childviewcontrollers);//    //Method Three//Uinavigationcontroller *nav = [[Uinavigationcontroller alloc]init];//[nav ADDCHILDVIEWCONTROLLER:ESSENCEVC];//[Essencevc.navigationcontroller PUSHVIEWCONTROLLER:NEWVC animated:yes];_window.rootviewcontroller=nav;            [_window makekeyandvisible]; returnYES;} The printing results are as follows: .- to- -  +: One:24.784nav.viewcontrollers: ("<WZEssenceViewController:0x7d178d00>",    "<WZNewViewController:0x7d179a20>",    "<WZFriendViewController:0x7d17a160>",    "<WZMeViewController:0x7d17a7f0>") Nav.childviewcontrollers: ("<WZEssenceViewController:0x7d178d00>",    "<WZNewViewController:0x7d179a20>",    "<WZFriendViewController:0x7d17a160>",    "<WZMeViewController:0x7d17a7f0>") 

[BS-20] The difference between the navigation controller and the view controller in adding controllers

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.