Several viewcontroller methods for iOS continuous dismiss

Source: Internet
Author: User

Original link: http://blog.csdn.net/longshihua/article/details/51282388

Presentviewcontroller is often used to show the Viewcontroller way, and the display and removal of Presentviewcontroller is also very simple, mainly the following two methods:

-(void) Presentviewcontroller: (Uiviewcontroller *) viewcontrollertopresent animated: (BOOL) flag completion: (void (^ __ Nullable) (void)) Completionns_available_ios (5_0);

-(void) dismissviewcontrolleranimated: (BOOL) flag completion: (void (^__nullable) (void)) Completionns_available_ios ( 5_0);

But sometimes our needs are very special, such as in a presentviewcontroller to present another viewcontroller, and even present a viewcontroller, Then maybe at some point the app sends out a message that needs to dismiss off all the viewcontroller at once, and go back to the first view controller, what should I do then? Take a look at the solution?

First, you have to know which Viewcontroller is the top of the entire app, and I'm going to build a parent view controller called Baseviewcontroller, and then record operations on that view Controller's viewwillappear. Use the Presentingviewcontroller property of the view controller to record the current view controller, and then for the view controller that needs to be present, inherit from Baseviewcontroller, and present a new view controller every time , the Viewwillappear method of the parent view controller will be executed:

-(void) viewwillappear: (BOOL) animated{        *Delegate= (appdelegate *) [[uiapplication Sharedapplication]delegate];       delegate. Presentingcontroller = self ;    }  


Finally, in the place where processing time is required (e.g. click events), add the following code to the Click event method to return to the original View Controller Display page:

- (void) Clickbutton: (ID) sender {appdelegate*Delegate= (Appdelegate *) [[uiapplicationsharedapplication]Delegate]; if(Delegate. Presentingcontroller) {Uiviewcontroller*VC =Self.presentingviewcontroller; if(!vc.presentingviewcontroller)return; //Loop get present out the view controller of this view controller (simply understand is the superior view controller), all the way to the bottom, then in the dismiss, then OK!          while(Vc.presentingviewcontroller) {VC=Vc.presentingviewcontroller; } [VC dismissviewcontrolleranimated:yescompletion:^{                        }]; }  } 


Attribute parsing:

Presentedviewcontroller:the view Controller, which is presented by this view Controlller (read-only), View Controller present by this view controller (read only)

Presentingviewcontroller:the View Controller that presented this view controller. (read-only), present out view controller of this view controller (read only)

Several viewcontroller methods for iOS continuous dismiss

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.