消除模态视图层次结构——一个优雅的解决方案
我有一个像这样的视图层次结构:
- 从应用程序委托中显示的自定义视图控制器(例如视图控制器 1)。
- 另一个视图控制器(比如视图控制器 2)从视图控制器 1 模态显示。
- 另一个视图控制器(比如视图控制器 3)从视图控制器 2 模态显示。
- 还有一个视图控制器(比如视图控制器 4)我需要遵循步骤2& 3.
现在,在某些活动中,我需要取消整个模式视图层次结构。所以我正在做的是,我从视图控制器 3 发送有关该事件的通知,并在视图控制器中侦听该通知,然后执行:
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"cancelViews" object:nil];
这可行,但有一个问题。根据我的应用程序流程,我最终在视图控制器 1 和视图控制器 4 上注册此通知。现在,当视图控制器 1 的整个模式层次结构启动并且通知由 VC1 注册,然后在另一个视图层次结构之上注册时显示了视图控制器 4,最后当引发通知时,我的视图层次结构都消失了。
我只希望最顶层的视图层次结构消失。任何解决方案都会真正有帮助。
I have a view hierarchy like this:
- A custom view controller (Say view controller 1) shown from app delegate.
- Another view controller (Say view controller 2) shown modally from view controller 1.
- Another view controller (Say view controller 3) shown modally from view controller 2.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 代表!
如果这不是您想要采取的路线(尽管我强烈鼓励您这样做),您可以设置 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.