在alertview中选择取消按钮时如何从数组中关闭所有alertview?

Posted

技术标签:

【中文标题】在alertview中选择取消按钮时如何从数组中关闭所有alertview?【英文标题】:How to dismiss all the alertview from an array when select the cancel button in alertview? 【发布时间】:2016-04-26 06:03:45 【问题描述】:

在警报视图中选择取消按钮时如何从数组中关闭所有警报视图?我在一个数组中有 5 个警报视图。如果我从第一个警报视图中选择取消,那么它将需要关闭剩余的所有警报而不是显示。

for (NSDictionary *temp in [RMUserDefaults userDetails].SharedFolders)
    
        NSString *name = temp[@"Name"];

        sharedFolderId = [RMUserDefaults userDetails].SharedFolders[0][@"id"];

        alert1= [[CustomUIAlertView alloc]initWithTitle:LString(@"RECEIPT_MATCH") message:[NSString stringWithFormat:@"%@ has SharedFolders you to a Team Plan.", name] delegate:self cancelButtonTitle:LString(@"CANCEL") otherButtonTitles:[NSMutableArray arrayWithObjects:LString(@"Upgrade Now"),nil]];
        alert1.tag = 12365;
        [alert1 show];
        double delayInSeconds = 5.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void)
        );
    

【问题讨论】:

【参考方案1】:

您可以使用以下代码关闭 UIAlertView。

UIWindow *window = [UIApplication sharedApplication].keyWindow;
for (UIView *view in w.subviews) 
    if ([view isKindOfClass:[UIAlertView class]]) 
        [(UIAlertView *)view dismissWithClickedButtonIndex:[(UIAlertView *)view cancelButtonIndex] animated:YES];
    

希望对你有帮助。

【讨论】:

UIAlertView *topAlert = [NSClassFromString(@"_UIAlertManager") performSelector:@selector(topMostAlert)] no 不行,我最近的项目尝试了各种方法,都行不通【参考方案2】:
 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil]; 
        [alert1 show];
        [self performSelector:@selector(dismiss:) withObject:alert1 afterDelay:1.0];

添加关闭方法

   -(void)dismiss:(UIAlertView*)alert
    
        [alert dismissWithClickedButtonIndex:0 animated:YES];
    

【讨论】:

【参考方案3】:
UIWindow *Mywindow = [UIApplication sharedApplication].keyWindow;

for (UIView *costumview in w.subviews) 

    if ([view isKindOfClass:[UIAlertView class]])
    
        [(UIAlertView *)view dismissWithClickedButtonIndex:[(UIAlertView *)view cancelButtonIndex] animated:YES];
    

【讨论】:

以上是关于在alertview中选择取消按钮时如何从数组中关闭所有alertview?的主要内容,如果未能解决你的问题,请参考以下文章

在 iOS 8.1 之后,在所有设备上,当点击 alertview 取消按钮时,ScrollView 向上并且不滚动

iphone Mail App Popup 样式 alertview

允许在数组中选择一个UIButton并取消选择其他

通过点击外部或取消按钮在 iOS 中关闭警报视图

如何在目标c中的tableview上方实现一个选择/取消选择所有按钮?

如何从视图模型中关闭 Catel 框架数据窗口