56

Is there a way to check if a modal view is present? I'd like to run a method only if a modal view is present. Also, if I have multiple modal views, is there a way to check if a certain modal view is present.

I use the following code to present and dismiss modal views:

    [self presentModalViewController:myModalView animated:YES];
    [self dismissModalViewControllerAnimated:YES];

Thank you in advance!

Cheers, Evan

PS. My modal view has a view controller, but I'd like to check if the modal view is present from a separate class that is running asynchronously.

Evan Johnson
  • 1,444
  • 1
  • 14
  • 31

4 Answers4

76

Are you checking the presence of a modal view controller from the parent view controller? If so, you can just check that view controller's modalViewController property:

BOOL modalPresent = (self.modalViewController);

If you want to check for a particular modal view controller, you can get the modal view controller's class name like this:

NSString *modalClassName = NSStringFromClass([self.modalViewController class]);
arlomedia
  • 8,534
  • 5
  • 60
  • 108
  • 59
    self.modalViewController is now deprecated, you should now use BOOL modalPresent = (BOOL)(self.presentedViewController); – allaire Apr 26 '12 at 19:33
  • 2
    To piggyback on @allaire's comment, here's the documentation on presentedViewController (http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instp/UIViewController/presentedViewController) that seems to suggest a modal view controller exists only if it is being presented by "this" view. – Danny May 29 '13 at 23:46
64

You can check using: self.presentedViewController, which returns The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy.

tipycalFlow
  • 7,594
  • 4
  • 34
  • 45
6

What worked for me is following:

// this is the trick: set parent view controller as application's window root view controller
UIApplication.sharedApplication.delegate.window.rootViewController = viewController;

// assert no modal view is presented
XCTAssertNil(viewController.presentedViewController);

// simulate button tap which shows modal view controller
[viewController.deleteButton sendActionsForControlEvents:UIControlEventTouchUpInside];

// assert that modal view controller is presented
XCTAssertEqualObjects(viewController.presentedViewController.class, MyModalViewController.class);

As far as I tested it, this works for iOS7 and iOS8. Didn't try on iOS6 however.

mixtly87
  • 1,675
  • 15
  • 32
  • 1
    Don't forget to add the UINavigationController as the window's rootViewController if your UIViewController is embedded in one. Then just call presentedViewController on that navController. :) – Joe Susnick May 18 '17 at 17:28
1

You can check the presence of a modal view controller from the parent view controller

if ( [[self presentingViewController] presentingViewController] ) {

}
jose920405
  • 7,982
  • 6
  • 45
  • 71
Binoy jose
  • 461
  • 4
  • 9