模态显示/关闭视图控制器时通知? [英] Notification when view controller is presented modally/dismissed?

查看:96
本文介绍了模态显示/关闭视图控制器时通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某个视图控制器以模态形式显示在另一个视图控制器之上(除了viewWillDisappear,显然不是iPad上的非全屏模式呈现方式,viewWillDisappear除外),是否有任何自动通知方式?

Is there any way to be notified automatically if some view controller is presented modally on top of another view controller (other than viewWillDisappear, which is obviously not called for non-fullscreen modal presentation on iPad)?

背景/用例:在iPad应用程序中,每当显示模式表单时,我都希望调整模式后面的视图外观.但是,模态的表示不一定在需要调整的同一视图控制器中完成,而是可以在某些子级或父级VC中发生.当然,我可以手动通知此事件(通过通知,委托或其他方式),但这很重复,以后将来添加更多的模式表单时很容易忘记.

Background/use case: In an iPad app, whenever a modal form sheet is presented, I want to adjust the appearance of the view behind the modal. However, the presentation if the modals is not necessarily done in the same view controller that needs to adapt, but can rather happen in some child or parent VCs. Of course, I could just communicate this event manually (via notification, delegation or whatever), but that's quite repetitive and can easily be forgotten when adding further modal form sheets in the future.

因此,我想知道是否还有其他更优雅的方法来实现这一目标.我已经在presentedViewController上尝试了KVO,但这似乎行不通(而且我也不确定如果模态表示是由子视图控制器完成的,实际上是否会设置presentedViewController).

Thus, I'm wondering if there is any more elegant way to achieve this. I've tried KVO on presentedViewController, but that doesn't seem to work (and I'm also not sure if presentedViewController will actually be set if the modal presentation is done by a child view controller...).

推荐答案

您可以在UIViewController上创建类别,并具有自己的 presentModalWithNotification 方法.这样,您就永远不会忘记发送通知,您可以订阅此通知并在需要时使用它.

You can create a category on UIViewController, and have your own method to presentModalWithNotification. This way you never forget to send the notification, and you can subscribe to this notification and use it whenever you need it.

这篇关于模态显示/关闭视图控制器时通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆