问题
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