Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if there is a modal UIViewController presented?

Is there a way to tell whether there is a modal UIViewController presented already, say, before calling dismissModalViewControllerAnimated?

like image 698
ohho Avatar asked Aug 09 '11 08:08

ohho


2 Answers

iOS 9, 8, 7, 6 & 5

There are just too many answers to this question, none covering all cases. Furthermore, despite what you find in the documentation, there are two alternatives to the now deprecated modalViewController:

  1. If you need to know if you are modal:

    BOOL modal = nil != [self presentingViewController];

  2. If you need to know if you are covered by a modal:

    BOOL hiddenByModal = nil != [self presentedViewController];

like image 160
SwiftArchitect Avatar answered Sep 18 '22 00:09

SwiftArchitect


iOS6+ - use presentedViewController: Since iOS 6, presentedViewController should be used instead as the modalViewController which has been deprecated

Use the property:

Deprecated - modalViewController: The controller for the active modal view—that is, the view that is temporarily displayed on top of the view managed by the receiver. (read-only)

@property(nonatomic, readonly) UIViewController *modalViewController
like image 27
Praveen S Avatar answered Sep 20 '22 00:09

Praveen S