Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismissing Multiple View Controllers in iOS 5

In iOS 4, if you want to dismiss two nested modal view controllers, the following code works:

[[[[self parentViewController] parentViewController] parentViewController] dismissModalViewControllerAnimated:YES];

However in iOS 5 this method no longer works. Does anybody know how to achieve this result in iOS 5?

like image 793
Andrew Lauer Barinov Avatar asked Dec 01 '22 01:12

Andrew Lauer Barinov


2 Answers

If you call dismissViewControllerAnimated: on the view controller that presented the first modal, you'll dismiss of both modals at once. So in your 2nd modal you would do the following:

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
like image 124
Karl Monaghan Avatar answered Dec 06 '22 23:12

Karl Monaghan


I have an app that closes nested modal view controllers through NSNotificationCenter. The notification is received by the VC that I want to navigate back to and all the VC's in between are gone.

In the deeper VC...

NSNotification * notification = [NSNotification notificationWithName:@"BACKTOINDEXNOTE" object:nil];
[[NSNotificationCenter defaultCenter] postNotification:notification];

In the VC I would like to go back to

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   if (self) {
       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismiss) name:@"BACKTOINDEXNOTE" object:nil];

       // more init code
   }
   return self;
}

-(void)dismiss
{
  [self dismissModalViewControllerAnimated:YES];
}

This works on iOS 5 device with a project deployed for 4.0+ I hope it helps. If you use this, it will scale to support more VC's in between your current VC and the one you want to dismiss to, without changing this code

like image 20
Jesse Black Avatar answered Dec 07 '22 00:12

Jesse Black