在iOS 5中关闭多个视图控制器

And*_*nov 3 objective-c modalviewcontroller ios ios5

在iOS 4中,如果要关闭两个嵌套的模态视图控制器,则以下代码有效:

[[[[self parentViewController] parentViewController] parentViewController] dismissModalViewControllerAnimated:YES];
Run Code Online (Sandbox Code Playgroud)

但是在iOS 5中,此方法不再有效.有谁知道如何在iOS 5中实现这个结果?

Kar*_*han 10

如果在呈现第一个模态的视图控制器上调用dismissViewControllerAnimated:,您将立即关闭两个模态.因此,在您的第二个模态中,您将执行以下操作:

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
Run Code Online (Sandbox Code Playgroud)


Jes*_*ack 8

我有一个应用程序通过NSNotificationCenter关闭嵌套的模态视图控制器.VC收到我想要导航回来的通知,其间的所有VC都消失了.

在更深层的VC ......

NSNotification * notification = [NSNotification notificationWithName:@"BACKTOINDEXNOTE" object:nil];
[[NSNotificationCenter defaultCenter] postNotification:notification];
Run Code Online (Sandbox Code Playgroud)

在VC我想回去

- (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];
}
Run Code Online (Sandbox Code Playgroud)

这适用于iOS 5设备,项目部署为4.0+我希望它有所帮助.如果你使用它,它将扩展以支持你当前的VC和你想要关闭的VC之间的更多VC,而无需更改此代码