简体   繁体   中英

Is there a way to know if a UIViewController has been presented and dismissed modally ?

Is there a way to know if a UIViewController has been presented and dismissed modally ?

Something like:

  • hasBeenPresentedModally
  • hasBeenDismissedModally

thanks

There's nothing built in, but a view controller could, upon receiving viewDidAppear and/or viewWillDisappear check whether it has a parentViewController , since per Apple's documentation (emphasis added):

Parent view controllers are relevant in navigation, tab bar, and modal view controller hierarchies. In each of these hierarchies, the parent is the object responsible for displaying the current view controller. If you are using a view controller as a standalone object—that is, not as part of a view controller hierarchy—the value in this property is nil.

If it has then it can set suitable flags for future reference.

Note that being presented modally is different from being truly modal. For example, on an iPad you might put one controller inside a UIPopoverController , so that controller isn't presented modally, but then it might modally present another controller on top of itself. So the second controller is presented modally but isn't itself a modal dialogue because — if the program is otherwise set up suitably — the user can just ignore the popover entirely.

Check if your UIViewController's parentViewController property is nil or not. If the property is nil then it's dismissed otherwise it's presented.

NOTE: UITableViewController's childViewController's parentViewController property would also be not nil , you should also make sure the parentViewController is not UITableViewController.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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