iOS 在显示另一个之前关闭 UIAlertView
我有一个 Utils 类,它在触发某些通知时显示 UIAlertView 。有没有一种方法可以在显示新的 UIAlertView 之前关闭任何打开的 UIAlertView?
目前,当应用程序进入后台时,我正在使用
[self checkViews:application.windows];
applicationDidEnterBackground
- (void)checkViews:(NSArray *)subviews {
Class AVClass = [UIAlertView class];
Class ASClass = [UIActionSheet class];
for (UIView * subview in subviews){
if ([subview isKindOfClass:AVClass]){
[(UIAlertView *)subview dismissWithClickedButtonIndex:[(UIAlertView *)subview cancelButtonIndex] animated:NO];
} else if ([subview isKindOfClass:ASClass]){
[(UIActionSheet *)subview dismissWithClickedButtonIndex:[(UIActionSheet *)subview cancelButtonIndex] animated:NO];
} else {
[self checkViews:subview.subviews];
}
}
}
执行此操作,这使得 applicationDidEnterBackground 变得很容易,因为我可以使用 application.windows
我可以使用 AppDelegate 或类似的东西来获取所有视图,循环遍历它们并关闭任何 UIAlertViews 吗?
I have a Utils class which shows UIAlertView when certain notifications are triggered. Is there a way to dismiss any open UIAlertViews before showing a new one?
Currenty I am doing this when the app enters the background using
[self checkViews:application.windows];
on applicationDidEnterBackground
- (void)checkViews:(NSArray *)subviews {
Class AVClass = [UIAlertView class];
Class ASClass = [UIActionSheet class];
for (UIView * subview in subviews){
if ([subview isKindOfClass:AVClass]){
[(UIAlertView *)subview dismissWithClickedButtonIndex:[(UIAlertView *)subview cancelButtonIndex] animated:NO];
} else if ([subview isKindOfClass:ASClass]){
[(UIActionSheet *)subview dismissWithClickedButtonIndex:[(UIActionSheet *)subview cancelButtonIndex] animated:NO];
} else {
[self checkViews:subview.subviews];
}
}
}
This makes it easy on applicationDidEnterBackground as I can use application.windows
Can I use the AppDelegate or anything similar to get all the views, loop through them and dismiss any UIAlertViews?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
iOS6兼容版本:
iOS6 compatible version:
iOS7兼容版本:
我制作了一个类别接口,将所有实例存储在init方法中。
我知道这是一种非常低效的方式。
在使用 UIAlertView 之前启动实例监控。
在显示另一个之前调用dismissAll。
如果您可以控制所有 UIAlertView,那么最好使用单例模式。
但就我而言,我需要此代码来关闭 UIWebView 中的 javascript 警报对话框。
iOS7 compatible version:
I made a category interface that stores all instance in init method.
I know it's a very inefficient way.
Start instance monitoring before using UIAlertView.
Call dismissAll before showing another.
It's better using a singleton pattern if you can control all UIAlertViews.
But in my case, I need this code for closing javascript alert dialog in a UIWebView.
由于
UIAlertView
在 iOS8 中已被弃用,取而代之的是UIAlertController
(这是一个UIViewController
,以模态方式呈现),因此您无法在同时(至少来自同一个 viewController)。第二个警报将不会出现。我想部分模拟
UIAlertView
的行为,并防止一次显示多个警报。下面是我的解决方案,它使用窗口的 rootViewController 来显示警报(通常是 appDelegate 的导航控制器)。我在 AppDelegate 中声明了这一点,但您可以将其放在您想要的位置。如果您在使用过程中遇到任何问题,请在评论中报告。
Since
UIAlertView
is deprecated in iOS8 in favor ofUIAlertController
(which is aUIViewController
, presented modally), you can't preset 2 alerts at the same time (from the same viewController at least). The second alert will simply not be presented.I wanted to partially emulate
UIAlertView
's behavior, as well as prevent showing multiple alerts at once. Bellow is my solution, which uses window'srootViewController
for presenting alerts (usually, that is appDelegate's navigation controller). I declared this in AppDelegate, but you can put it where you desire.If you encounter any sorts of problems using it, please report here in comments.