iOS view controller navigation and life cycle research Demo

Source: Internet
Author: User

1. Background:

April 2014 First contact with iOS development to develop a financial app for a bank.
In the final phase of development, added the ability to return to the homepage directly from any page.
Sadly, instead of using Uinavigationcontroller for navigation management, I used the modal jump mode in iOS (presentviewcontroller/ Dismissviewcontrolleranimated).

So we need to find a way to do this, to achieve the following way of navigation jump:

Known: page a→ page b→ page C solution: page C jumps directly back to page A, and requires no memory leakage, cyclic dependence, such as 2, thinking:

In the face of the above requirements, the simplest way is to change all the controllers to Uinavigationcontroller, and use the pushviewcontroller/poptoviewcontroller/ Poptorootviewcontrolleranimated and other methods to solve the problem perfectly. However, at that time the project page of nearly 100, divided into three modules, the need for large-scale modification of the design page and the adjustment of a large number of code, this is not a realistic solution, not the last resort, can not take such a poor means!

We need a solution that meets the following criteria:

1) for the page already completed in the Interfacebuilder, do not make any changes 2) as little as possible to modify the code, because a lot of code has been tested by the testing center, if modified, need to re-test all, time too late.

After a two-day study, in-depth understanding of the jump process and life cycle in iOS, a relatively perfect solution was found to meet the requirements mentioned above. Through a demo, to share with you.

3, the problem-solving process:
1) To reduce the code changes, add a base class.
@interface baseviewcontroller:uiviewcontroller//prevent controller from looping references, using weak reference @property (Nonatomic,weak)   baseviewcontroller* parentcontroller;//to be able to understand a page controller life cycle related information, give the controller a name @property (nonatomic,copy) nsstring* ctrlname;//key function, make page c--> jump to homepage-(void) Dodismiss; @end
2   To better understand the life cycle of Viewcontroller in iOS, we output relevant information in the base class to understand lifecycle-related information. 
@implementation  baseviewcontroller-(ID)  init{    self.ctrlname = @ "";     self.parentController = nil;    return [super  INIT];} -  (void) Viewdidload{    nslog (@ "%@ invoke viewdidload", self.ctrlName);     [super viewdidload];    // do any additional  setup after loading the view.} -(void) Viewwillappear: (BOOL) Animated{    nslog (@ "%@ invoke viewwillappear", Self.ctrlname);     [super viewwillappear:animated];} -(void) Viewdidappear: (BOOL) Animated{    nslog (@ "%@ invoke viewdidappear", Self.ctrlname);     [super viewdidappear:animated];} -(void)  viewwilldisappear: (BOOL) Animated{    nslog (@ "%@ invoke  Viewwilldisappear ", Self.ctrlname);    [super viewwilldisappear:animated];} -(void)  viewdiddisappear: (BOOL) Animated{    nslog (@ "%@ invoke  Viewdiddisappear ", self.ctrlname);     [super viewdiddisappear:animated];} -  (void) Didreceivememorywarning{    nslog (@ "%@ invoke  Didreceivememorywarning ", Self.ctrlname);    [super didreceivememorywarning];     // dispose of any resources that can be recreated.} -(void) Dealloc{    nslog (@ "%@ .......dealloc ...", self.ctrlname);}
3   Key recursive function, the core is to understand the dismissviewcontrolleranimated:completion function in the timing of the completion callback, this is the key to solve the problem. 
-(void) Dodismiss{    nslog (@ "%@ dismiss begin", Self.ctrlname);     if ([Self.parentcontroller iskindofclass:[viewcontroller class]])     {         [self dismissviewcontrolleranimated:yes completion:  ^ (void)          {              nslog (@ "%@ dismiss end", Self.ctrlname);              if (Self.parentcontroller)               {                  [self.parentController doDismiss];              }         } ];   &nbsP;}     else    {        [self  dismissviewcontrolleranimated:no completion: ^ (void)           {             nslog (@ "%@  Dismiss end ", Self.ctrlname);              if (Self.parentcontroller)              {                  [ self.parentcontroller dodismiss];              }         }];    }}
4) The Demo page structure, such as: Mainviewcontroller-->secondviewcontroller-->child1viewcontroller, and then jump directly back to MAINVIEWCONTROLLER.5 All Viewcontroller are inherited from our custom baseviewcontroller, with the key Dodismiss recursion Method!

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/8D/91/wKiom1ihn9mxbH7NAAAjGYPukfU106.png-wh_500x0-wm_ 3-wmp_4-s_4201047928.png "title=" 1.png "alt=" Wkiom1ihn9mxbh7naaajgypukfu106.png-wh_50 "/>

6) Look at the information from the Mainviewcontroller-->secondviewcontroller-->child1viewcontroller-->mainviewcontroller life cycle:

a, start the program, enter Mainviewcontroller:
2017-02-11 17:25:16.964 navtest[933:17086] Mainviewctrl invoke Viewdidload
2017-02-11 17:25:16.966 navtest[933:17086] Mainviewctrl invoke Viewwillappear
2017-02-11 17:25:16.975 navtest[933:17086] Mainviewctrl invoke Viewdidappear

b, from Mainviewcontroller→secondviewcontroller:
2017-02-11 17:28:29.717 navTest[ 933:17086] Secondviewcontroller invoke Viewdidload [Newcontroller call first viewdidload]
2017-02-11 17:28:29.721 navtest[933:17086] Mainviewctrl invoke Viewwilldisappear [then Oldcontroller call viewwilldisappear]< /strong>
2017-02-11 17:28:29.722 navtest[933:17086] Secondviewcontroller invoke Viewwillappear [ Then Newcontroller calls Viewwillappear]
2017-02-11 17:28:30.229 navtest[933:17086] Secondviewcontroller Invoke Viewdidappear [then Newcontroller call Viewdidappear]
2017-02-11 17:28:30.229 navtest[ 933:17086] Mainviewctrl invoke Viewdiddisappear [then Oldcontroller call Viewdiddisappear]
2017-02-11 17:28:30.230 navtest[933:17086] Mainviewctrl present End
[last Oldcontroller call Presentviewcontroller completion callback called ]

The following is the output code of Presentviewcontroller completion callback information, to understand at which stage the completion is called, very important information Oh!!!

-(Ibaction) Clicktoentersecondcontroller: (ID) Sender {secondviewcontroller* CTRL = [Self.storyboard    instantiateviewcontrollerwithidentifier:@ "Second"];    Ctrl.parentcontroller = self;    Ctrl.ctrlname = @ "Secondviewcontroller";     [Self Presentviewcontroller:ctrl animated:yes completion:^ (void)     {NSLog (@ "%@ present End", Self.ctrlname); }];}

C, the process of returning from Child1viewcontroller directly to Mainviewcontroller (skip Secondviewcontroller)

-(Ibaction) Returnmainviewcontroller: (ID) Sender {NSLog (@ "**********************dodismiss*********************"); [Self Dodismiss];}

D, 2017-02-11 17:30:30.634 navtest[933:17086]**Dodismiss* [to start calling the Dodismiss function, which means to click the Back button]
2017-02-11 17:30:30.635 navtest[933:17086] Child1viewcontroller dismiss begin[output dismiss begin, representing a dodismiss recursive function called Child1viewcontroller]
2017-02-11 17:30:30.637 navtest[933:17086] Child1viewcontroller invoke Viewwilldisappear[Oldcontroller Viewwilldisappear]
2017-02-11 17:30:30.638 navtest[933:17086] Secondviewcontroller invoke Viewwillappear[Newcontroller Viewwillappear]
2017-02-11 17:30:30.641 navtest[933:17086] Secondviewcontroller invoke Viewdidappear[Newcontroller Viewdidappear]
2017-02-11 17:30:30.641 navtest[933:17086] Child1viewcontroller invoke Viewdiddisappear[Oldcontroller Viewdiddisappear]
2017-02-11 17:30:30.641 navtest[933:17086] Child1viewcontroller dismiss end[Critical Moment Oh, Oldcontroller self dismissviewcontrolleranimated:completion: In the completion callback was triggered, it was in Oldcontroller Viewdiddisappear is triggered after the "Oh"
2017-02-11 17:30:30.641 navtest[933:17086] Secondviewcontroller dismiss begin[At this point, the process of bouncing from Childviewcontroller to Secondviewcontroller is done, and then recursively called, To complete the process of bouncing back from Secondviewcontroller to Mainviewcontroller, repeat the process above]
2017-02-11 17:30:30.642 navtest[933:17086] child1viewcontroller ..... dealloc .....[Child1viewcontroller has completely disappeared, so the memory is destroyed so that the memory is not compromised.]

The following is the recursive section, just like the above process, only jumps back from Secondviewcontroller to Mainviewcontroller

2017-02-11 17:30:30.643 navtest[933:17086] Secondviewcontroller invoke Viewwilldisappear
2017-02-11 17:30:30.644 navtest[933:17086] Mainviewctrl invoke Viewwillappear
2017-02-11 17:30:31.156 navtest[933:17086] Mainviewctrl invoke Viewdidappear
2017-02-11 17:30:31.156 navtest[933:17086] Secondviewcontroller invoke Viewdiddisappear
2017-02-11 17:30:31.156 navtest[933:17086] Secondviewcontroller dismiss end
2017-02-11 17:30:31.157 navtest[933:17086] Mainviewctrl dismiss begin
2017-02-11 17:30:31.157 navtest[933:17086] secondviewcontroller ..... dealloc ..... [Secondviewcontroller memory is also very well-destroyed, the only remaining mainviewcontroller is still alive]

Show me the effects of a previous simulator in a bank app, and iOS development is a very enjoyable experience!

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/8D/8F/wKioL1ihoAnxYJ8wAAHAMiHDBGk898.png-wh_500x0-wm_ 3-wmp_4-s_2269591719.png "title=" 6976b933d3e1a75aa556c2bfef934525.png "alt=" Wkiol1ihoanxyj8waahamihdbgk898.png-wh_50 "/>


SOURCE Download:

IOS Uiviewcontroller navigation and life cycle Demo

This article from "with the wind and the green shirt minded Xianfeng line" blog, reprint please contact the author!

iOS view controller navigation and life cycle research Demo

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.