Question

I have 3 viewControllers - BaseViewController->AviewController->BviewController.

AviewController is presented modally on BaseViewController and BviewController is presented modally on AviewController.

In AviewController if I call [self dismissViewControllerAnimated:Completion] it dismisses both AviewController and BviewController.

In BviewController if I call [self.presentingViewController dismissViewControllerAnimated:completion] it only dismisses BviewController. Why is AviewController not dismissed ?

Is this concept wrong AviewController = BviewController.presentingViewController. ?

I also tried taking weak reference of AviewController in BviewContrroller and tried to dismiss. But still only BviewController alone is dismissed. i.e [AviewControllerReference dismissViewControllerAnimated:completion]

Any mistake in my understanding of the concept?

Was it helpful?

Solution

From apple docs:

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.

So you must call this method on presenting VC to dismiss the presented controller. Not presented controller it self. As you can see in above in bold test, if you called this on a presented VC it automatically forwards the method to prsenting VC.

So to answer to your issue,

Call below line on BViewController to dismiss both AViewcontroller and BViewController. ,

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil]; 

this is Equalent to calling dismissViewController: on your BaseViewController. Which is the correct way of dismissing AViewController

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top