Is it possible to determine whether ViewController is presented as Modal?

后端 未结 14 2187
一生所求
一生所求 2020-12-04 07:13

Is it possible to check inside ViewController class that it is presented as modal view controller?

14条回答
  •  难免孤独
    2020-12-04 07:46

    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.

提交回复
热议问题