user3074248
user3074248

Reputation: 27

How to determine what view controller presented the current view controller? (iphone)

Depending on what view controller presented the current view controller, I need my current view controller to be dismissed by either of two presenting view controllers.

Here is the method in the current view controller:

-(void)goBack:(id)sender {
if ([self.presentingViewController.presentingViewController isKindOfClass:[InitialViewController class]])
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

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

}

It doesn't work though. What am I doing wrong? Thank you very much in advance!

Upvotes: 1

Views: 944

Answers (2)

matt
matt

Reputation: 535925

There's nothing wrong with your approach to dismissal (despite my earlier wrong answer!). What's wrong is your logic. The problem is this line:

if ([self.presentingViewController.presentingViewController isKindOfClass:[InitialViewController class]])

Change it to:

if ([self.presentingViewController isKindOfClass:[SecondViewController class])

That should do exactly what you want.

Upvotes: 2

Gavin
Gavin

Reputation: 8200

If you want to get the view controller that presented a particular view controller, just do viewController.presentingViewController. That should always be set to whatever view controller presented the current view controller, that's the point of it. So if you want to dismiss the current view controller, do:

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

Upvotes: 0

Related Questions