gpt4 book ai didi

ios - 当应用程序进入后台时如何在应用程序中全局隐藏 UIAlertController

转载 作者:行者123 更新时间:2023-12-01 20:09:57 27 4
gpt4 key购买 nike

我需要一个系统来隐藏所有UIAlertController当她进入后台时在我的应用程序中。目前我使用 BaseViewController我所有的类(class)UIViewControllers注册“进入后台”通知并引用 UIAlertViewController .这个BaseViewController如果弹出窗口可见,则隐藏它。它有效,但不实用。

另一种方法是子类化,但 Apple doc 说:

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.



您是否有更好的解决方案,不涉及子类 UIViewController谁包含 UIAlertController ?

最佳答案

请使用此代码,您需要获取顶 View Controller
使用此方法获取顶 View Controller

- (id)getTopViewController {

UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
if (![topController isKindOfClass:[NSNull class]]) {
return topController;
}
}


- (void)applicationDidEnterBackground:(NSNotification *)notification {

UIViewController *viewOb = [self getTopViewController];

// check UIViewController is UIAlertController
if ([view isKindOfClass:[UIAlertController class]]) {
NSLog(@"%@",view);
[viewOb dismissViewControllerAnimated:YES completion:nil];
}
}
然后用 hot viewController 关闭 ViewController

关于ios - 当应用程序进入后台时如何在应用程序中全局隐藏 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37960046/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com