Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dismiss viewController by using presentingViewController

Tags:

ios

iphone

ios5

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!

like image 947
passol Avatar asked Feb 14 '23 19:02

passol


2 Answers

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.

like image 84
Kevin Avatar answered Feb 25 '23 19:02

Kevin


see documentation:

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.

like image 25
ibamboo Avatar answered Feb 25 '23 19:02

ibamboo