iOS - 如何检查模式视图是否存在

发布于 2024-10-23 11:50:45 字数 320 浏览 1 评论 0原文

有没有办法检查模式视图是否存在?我想仅在存在模式视图时运行方法。另外,如果我有多个模态视图,有没有办法检查是否存在某个模态视图。

我使用以下代码来呈现和关闭模态视图:

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

提前谢谢您!

干杯, 埃文

PS.我的模态视图有一个视图控制器,但我想检查模态视图是否存在于异步运行的单独类中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凶凌 2024-10-30 11:50:45

您是否从父视图控制器检查模式视图控制器是否存在?如果是这样,您只需检查该视图控制器的 modalViewController 属性:

BOOL modalPresent = (self.modalViewController);

如果您想检查特定的模态视图控制器,您可以获取模态视图控制器的类名,如下所示:

NSString *modalClassName = NSStringFromClass([self.modalViewController class]);

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]);
静待花开 2024-10-30 11:50:45

您可以使用以下方法进行检查:self.presentedViewController,它返回此视图控制器呈现的视图控制器,或其在视图控制器层次结构中的祖先之一。

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.

花想c 2024-10-30 11:50:45

对我有用的是:

// 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);

据我测试,这适用于 iOS7 和 iOS8。不过iOS6没试过。

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.

丿*梦醉红颜 2024-10-30 11:50:45

您可以从父视图控制器检查模态视图控制器是否存在

if ( [[self presentingViewController] presentingViewController] ) {

}

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

if ( [[self presentingViewController] presentingViewController] ) {

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文