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