UIAlertView didmissWithClickedButtonIndex不会忽略警报

时间:2012-05-10 15:22:01

标签: iphone uialertview

从我处理Web服务连接的对象,当网络出现故障时,我将警报传递给使用Web服务对象的视图控制器。

WebServiceObject:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Connection failed!  You must be connected to a Wifi source to download data.  Please reconnect to a Wifi source and try again later."] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    NSDictionary *alertDict = [NSDictionary dictionaryWithObjectsAndKeys:alert, @"AlertView", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:DisplayAlertNotification object:self userInfo:alertDict];

的ViewController:

- (void)displayAlert:(NSNotification *)notification {
    NSDictionary *dict = [notification userInfo];
    if ([[dict objectForKey:@"AlertView"] isKindOfClass:[UIAlertView class]]) {
        UIAlertView *alert = [dict objectForKey:@"AlertView"];
                NSNumber *theTag = [dict objectForKey:@"AlertTag"];
    NSLog(@"%i", [theTag integerValue]);
    alert.tag = [[dict objectForKey:@"AlertTag"] integerValue];
        [alert show];
    }
}


- (void)removeAlert:(NSNotification *)notification {
    NSDictionary *dict = [notification userInfo];
    if ([[dict objectForKey:@"AlertTag"] isKindOfClass:[NSNumber class]]) {
        NSNumber *theTag = [dict objectForKey:@"AlertTag"];
        UIAlertView *alert = (UIAlertView *)[self.view viewWithTag:[theTag integerValue]];
        // Not sure why but my alert is nil at this point
        [alert dismissWithClickedButtonIndex:0 animated:YES];
    }
}

我也以相同的方式使用removeAlert方法以编程方式删除警报。如果网络出现故障,但用户没有单击“确定”,然后网络重新启动,我将解除网络失败警报,并显示“网络已恢复”警报。它可以工作,除非它解除警报并显示网络已恢复,一旦用户在网络恢复时单击确定,原始网络失败只会恢复一次。如果用户在显示网络失败时单击确定,则它永远不会恢复。

我是否正确地以这种方式解除警报?感谢。

编辑:我可以通过在WebServiceObject中保存引用并以此方式解除它来使其工作。

1 个答案:

答案 0 :(得分:1)

您将警报设置为nil,因此它不执行任何操作

alert = nil;

[alert dismissWithClickedButtonIndex:0 animated:YES];