Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

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

Something like:

  • hasBeenPresentedModally
  • hasBeenDismissedModally

thanks

like image 396
aneuryzm Avatar asked Sep 05 '11 07:09

aneuryzm


People also ask

How do I dismiss a presented view controller in Swift?

Try to use modal. If you use push, you should dismiss it with the pop method of the navigation controller.

How do I dismiss a presented ViewController?

When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it.

Which of the following method is called after UIViewController is initialized?

First UIViewController is alloc'ed by some other object, then init is immediately called (or some other init method, like initWithStyle). Only once the object is initialized would I expect it to call its own loadView function, after which the view, once loaded, calls the viewDidLoad delegate method.

What is UIViewController life cycle?

The LifecycleThe view controller lifecycle can be divided into two big phases: the view loading and the view lifecycle. The view controller creates its view the first time the view is accessed, loading it with all the data it requires. This process is the view loading.


2 Answers

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.

like image 66
Tommy Avatar answered Sep 24 '22 00:09

Tommy


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.

like image 36
xuzhe Avatar answered Sep 22 '22 00:09

xuzhe