简体   繁体   English

解雇模态视图层次结构—优雅的解决方案

[英]Dismissal of modal view hierarchy — An elegant solution

I have a view hierarchy like this: 我有一个这样的视图层次结构:

  1. A custom view controller (Say view controller 1) shown from app delegate. 从应用程序委托显示的自定义视图控制器(说视图控制器1)。
  2. Another view controller (Say view controller 2) shown modally from view controller 1. 从视图控制器1模态显示的另一个视图控制器(说视图控制器2)。
  3. Another view controller (Say view controller 3) shown modally from view controller 2. 从视图控制器2模态显示的另一个视图控制器(说视图控制器3)。
  4. There is one more view controller (Say view controller 4) from which I need to follow step 2 & 3. 还有一个视图控制器(说视图控制器4),我需要从中执行步骤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: 所以我正在做的是,我正在从视图控制器3发送关于该事件的通知,并在视图控制器中侦听该通知,然后执行:

[[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. 按照我的应用程序流程,我最终要在视图控制器1和视图控制器4上注册此通知。现在,当视图控制器1的整个模式层次结构启动并且VC1注册了通知,然后在另一个视图层次结构之上显示了视图控制器4的视图,最后在引发通知时,我的视图层次结构都消失了。

I want only my top most view hierarchy to be disappeared. 我只希望最顶层的视图层次结构消失。 Any solution for this will really help. 对此的任何解决方案都将真正有帮助。

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. 如果这不是您要采取的路线(即使我极力鼓励您这样做),则可以设置1和4视图控制器以发布根视图控制器将侦听的不同事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM