iOS - How to check if a modal view is present

后端 未结 4 911
傲寒
傲寒 2021-01-30 05:13

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

相关标签:
4条回答
  • 2021-01-30 05:28

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

    if ( [[self presentingViewController] presentingViewController] ) {
    
    }
    
    0 讨论(0)
  • 2021-01-30 05:39

    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.

    0 讨论(0)
  • 2021-01-30 05:44

    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]);
    
    0 讨论(0)
  • 2021-01-30 05:55

    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.

    0 讨论(0)
提交回复
热议问题