iOS - 如何检查模态视图是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5338049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - How to check if a modal view is present
提问by Evan Johnson
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.
附注。我的模态视图有一个视图控制器,但我想检查模态视图是否存在于异步运行的单独类中。
回答by arlomedia
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:
您是否从父视图控制器检查模态视图控制器的存在?如果是这样,您可以检查该视图控制器的 modalViewController 属性:
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]);
回答by tipycalFlow
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.
您可以检查使用:self.presentedViewController
,它返回The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy.
回答by mixtly87
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.
据我测试,这适用于 iOS7 和 iOS8。然而没有在iOS6上尝试。
回答by Binoy jose
You can check the presence of a modal view controller
from the parent view controller
您可以view controller
从父级检查模态的存在view controller
if ( [[self presentingViewController] presentingViewController] ) {
}