消除模态视图层次结构——一个优雅的解决方案

发布于 2024-11-03 05:09:26 字数 593 浏览 5 评论 0原文

我有一个像这样的视图层次结构:

  1. 从应用程序委托中显示的自定义视图控制器(例如视图控制器 1)。
  2. 另一个视图控制器(比如视图控制器 2)从视图控制器 1 模态显示。
  3. 另一个视图控制器(比如视图控制器 3)从视图控制器 2 模态显示。
  4. 还有一个视图控制器(比如视图控制器 4)我需要遵循步骤2& 3.

现在,在某些活动中,我需要取消整个模式视图层次结构。所以我正在做的是,我从视图控制器 3 发送有关该事件的通知,并在视图控制器中侦听该通知,然后执行:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"cancelViews" object:nil];

这可行,但有一个问题。根据我的应用程序流程,我最终在视图控制器 1 和视图控制器 4 上注册此通知。现在,当视图控制器 1 的整个模式层次结构启动并且通知由 VC1 注册,然后在另一个视图层次结构之上注册时显示了视图控制器 4,最后当引发通知时,我的视图层次结构都消失了。

我只希望最顶层的视图层次结构消失。任何解决方案都会真正有帮助。

I have a view hierarchy like this:

  1. A custom view controller (Say view controller 1) shown from app delegate.
  2. Another view controller (Say view controller 2) shown modally from view controller 1.
  3. Another view controller (Say view controller 3) shown modally from view controller 2.
  4. There is one more view controller (Say view controller 4) from which I need to follow step 2 & 3.

Now on some activity I need to cancel the whole modal view hierarchy. So what I am doing is, I am sending a notification on that event from view controller 3 and listeneing to that notification in view controller and then executing:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"cancelViews" object:nil];

This works but there is one problem. As per my app flow I am ending up registering for this notification at both view controller 1 and view controller 4. Now, when whole modal hierarchy for view controller 1 was up and notification was registered by VC1 and then on top of that another view hierarchy for view controller 4 is shown and finally when notification is thrown both my view hierarchy disappears.

I want only my top most view hierarchy to be disappeared. Any solution for this will really help.

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

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

发布评论

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

评论(1

场罚期间 2024-11-10 05:09:26

使用 代表

如果这不是您想要采取的路线(尽管我强烈鼓励您这样做),您可以设置 1 和 4 视图控制器来发布根视图控制器将侦听的不同事件。

Use delegates!

If that isn't a route you want to take (even though I strongly encourage you to do so) you can setup your 1 and 4 view controller to publish different events for which your root view controller will listen.

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