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

发布于 2024-12-26 03:33:09 字数 237 浏览 1 评论 0原文

在 iOS 4 中,如果您想关闭两个嵌套的模态视图控制器,可以使用以下代码:

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

但是在 iOS 5 中此方法不再有效。有谁知道如何在 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔少女心 2025-01-02 03:33:09

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

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

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];
↙温凉少女 2025-01-02 03:33:09

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

在更深入的 VC 中...

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

在 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];
}

This Works on iOS 5 device with a project returned for 4.0+
我希望它有帮助。如果您使用此功能,它将能够扩展以支持当前 VC 和您想要取消的 VC 之间的更多 VC,而无需更改此代码

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

段念尘 2025-01-02 03:33:09

对于两个模式的堆栈,从初始演示者的委托方法中调用这个婴儿,以跳回堆栈并破坏所呈现的 VC。

[self.presentedViewController.presentedViewController dismissViewControllerAnimated:NO completion:nil];
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];

显然它有点脆弱,因为如果你开始添加更多模态,那么事情就会崩溃。一般来说,如果你正在做一堆控制器,你会使用 UINavigationController,但对于几个模态,这可以解决问题,并且比设置通知甚至更多委托要简单得多!

For a stack of two modals call this baby from your delegate method on the initial presenter to jump back down the stack and nuke the presented VCs.

[self.presentedViewController.presentedViewController dismissViewControllerAnimated:NO completion:nil];
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];

Obviously it's a bit brittle because if you start adding more modals then things will break. Generally if you're doing a stack of controllers you would use UINavigationController, but for a couple of modals this does the trick and is a lot less complex than setting up notifications or even more delegates!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文