Notification when view controller is presented modally/dismissed?

谁说我不能喝 提交于 2020-01-17 05:16:27

问题


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)?

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.

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...).


回答1:


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.



来源:https://stackoverflow.com/questions/30040733/notification-when-view-controller-is-presented-modally-dismissed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!