检查uialertview是否正在显示
-
22-09-2019 - |
题
我有一种方法可以发布HTTP数据并在有错误时显示UialerTview。如果我有多个HTTP帖子,我将显示每个错误的多个UialertView。
我只想在不显示其他uialertView时才显示uialertView。我该如何确定?
解决方案
在调用的对象上,在uialertView上调用show方法之前,请设置IVAR。
...
if (!self.alertShowing) {
theAlert = [[UIAlertView alloc] initWithTitle:title message:details delegate:self cancelButtonTitle:nil otherButtonTitles:@"Okay", nil];
self.alertShowing = YES;
[theAlert show];
}
...
然后,在您的代表方法中,警报管理设置标志IVAR为否:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
...
self.alertShowing = NO;
}
如果您希望警报顺序显示,我将发布通知以将每个消息添加到队列中,然后在驳回警报后才将消息从队列中删除。
其他提示
为什么不仅检查由UialerTview类维护的可见属性呢?
if (_alert) //alert is a retained property
{
self.alert = [[[UIAlertView alloc] initWithTitle:@"Your Title"
message:@"Your message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK"] autorelease];
}
if (!_alert.visible)
{
[_alert show];
}
如果您可以控制其他警报视图,请检查 visible
每个人的财产。
在iOS 6或之前,出现警报时,它将移至_uialerToverlaywindow。因此,一种非常脆弱的方法是遍历所有窗口,并检查是否有任何UialertView子视图。
for (UIWindow* window in [UIApplication sharedApplication].windows) {
NSArray* subviews = window.subviews;
if ([subviews count] > 0)
if ([[subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]])
return YES;
}
return NO;
尽管苹果无法抱怨这一点,但这是无证的,因为它取决于内部视图层次结构。一种更可靠但更无证件的方法是检查 如果 [_UIAlertManager visibleAlert]
是零.
这些方法无法检查是否显示了Springboard的UialertView。
- (BOOL)checkAlertExist {
for (UIWindow* window in [UIApplication sharedApplication].windows) {
NSArray* subviews = window.subviews;
if ([subviews count] > 0) {
for (id cc in subviews) {
if ([cc isKindOfClass:[UIAlertView class]]) {
return YES;
}
}
}
}
return NO;
}
在整个应用程序中都起作用,并且不涉及行走视图堆栈的另一个选项是子类 UIAlertView
至 MyUIAlertView
, ,添加静态(类)变量 BOOL alertIsShowing
, ,覆盖 -(void)show
选择器。
在你的覆盖中 show
选择器,检查 alertIsShowing
多变的。如果它是 YES
然后在延迟后重试(使用 dispatch_after
或设置一个 NSTimer
)。如果它是 NO
, ,继续打电话 [super show]
并分配 YES
至 alertIsShowing
;当隐藏警报视图时,设置 alertIsShowing
回到 NO
(您需要聪明地处理代表)。
最后,遍历全部 UIAlertView
实例与 MyUIAlertView
.
我认为它会起作用:
-(BOOL) doesAlertViewExist {
if ([[UIApplication sharedApplication].keyWindow isMemberOfClass:[UIWindow class]])
{
return NO;//AlertView does not exist on current window
}
return YES;//AlertView exist on current window
}
迅速:
func showAlert(withTitle title: String, message: String, viewController: UIViewController) {
if viewController.presentedViewController == nil { // Prevent multiple alerts at the same time
let localizedTitle = NSLocalizedString(title, comment: "")
let localizedMessage = NSLocalizedString(message, comment: "")
let alert = UIAlertController(title: localizedTitle, message: localizedMessage, preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
alert.addAction(action)
viewController.presentViewController(alert, animated: true, completion: nil)
}
}
// initialize default flag for alert... If alert is not open set isOpenAlert as NO
BOOL isAlertOpen;
isAlertOpen = NO;
if (isAlertOpen == NO) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Alert is Open" delegate:self cancelButtonTitle:@"Okay!!" otherButtonTitles: nil];
[alert show];
// Now set isAlertOpen to YES
isAlertOpen = YES;
}
else
{
//Do something
}
+ (BOOL)checkAlertExist {
for (UIWindow* window in [UIApplication sharedApplication].windows) {
if ([window.rootViewController.presentedViewController isKindOfClass:[UIAlertController class]]) {
return YES;
}
}
return NO;
}
关于我寻求在视图层次结构中找到uialertview的一些注释:
我试图通过所有 [UIApplication sharedApplication].windows
递归查看,但找不到任何东西。
这 windows
财产的 UIApplication
文档指出以下内容:
此属性包含与应用程序当前关联的UIWindow对象。 此列表不包括系统创建和管理的窗口, ,例如用于显示状态栏的窗口。
所以这使我意识到 UIWindow
在哪里 UIAlertView
可以找到的甚至没有向我们介绍。
但是,还有一个财产 UIApplication
叫 keyWindow
. 。循环完成此操作后,我找到了可以构成警报视图的私人课程:
在iOS 7上: _UIModalItemHostingWindow
, _UIModalItemAlertContentView
, _UIBackdropEffectView
等等
在iOS 8上: _UIAlertControllerActionView
, _UIAlertControllerShadowedScrollView
, _UIBackdropView
等等
我找不到 UIAlertView
我介绍了,但相反,一堆在内部组成的课程。 因此回答原始问题, ,您可能可以使用 keyWindow
属性,看看您是否注意到这些类,但是您的应用程序可能会因尝试检查私人课程而被拒绝。
对于使用的人,较新, UIAlertController
可用于iOS 8可以使用以下方式获得对其的引用:[UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController
.