Ios – dismiss viewController by using presentingViewController

iosios5iphone

I have three ViewControllers and its order is (A present B which presents C),
when I stay in the C viewController ,I have to dismiss ViewController to B viewController.

for C viewController ,its presentingViewCOntroller is B viewController

of course ,I can use

[self dismissViewControllerAnimated:YES completion:NULL];//self means C ViewController

But I was wondering I can also use the following method:

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

because the presentingViewController of C is B ViewController, but it did the same effect.
self means C ViewController while self.presentingViewController means B ViewController,but they also did the same work

The second question is I cannot use the following to dismiss two viewController in succession:

  [self dismissViewControllerAnimated:YES completion:^{
    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}]; //self means C viewController

Thanks for your help!

Best Solution

of course ,I can use

[self dismissViewControllerAnimated:YES completion:NULL];//self means C ViewController

This only dismisses C, not B as you seem to want.

But I was wondering I can also use the following method:

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

Yes, this works. When in doubt, try it out.

The second question is I cannot use the following to dismiss two viewController in succession:

[self dismissViewControllerAnimated:YES completion:^{
    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}]; //self means C viewController

That's not a question. Anyway, the reason it doesn't work is that after you dismiss yourself, your presentingViewController is nil. You need to store it in a temporary variable.

UIViewController *gp = self.presentingViewController.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
    [gp dismissViewControllerAnimated:YES completion:nil];
    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}];

Of course, the two will have different animations, you'll need to decide which you prefer.

Related Question