Sunday, May 7, 2017

Which viewcontroller gets dismissed when calling iOS dismissViewController method

Apple document has the below information regarding the below method:
dismissViewControllerAnimated:completion: 
Dismisses the view controller that was presented modally by the view controller.

Discussion
The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.

This document is quite confusing as there are three view controllers are involved in the operation.
self  (this is the current viewController receiving the method call)
self.presentedViewController
self.presentingViewController

So in different cases, which view controller is really got dismissed?

A quick testing shows the actual logic is implemented as below:
When dismissViewControllerAnimated method is called on a ViewController object, it first checks whether the current viewControlller's presentedViewController contains a valid object, if so, then just dismisses the presentedViewController and returns.

If the current ViewController's presentedViewController is nil, then it will dismiss the current viewController. Internally by forwarding the message to its presentingViewController as Apple document mentions. 

So basically, the method will dismiss self.presentedViewController. If the self.presentedViewController is nil, then it will dismiss the current viewController which receives the method call.


No comments:

Post a Comment