Iphone UIAlertView didmissWithClickedButtonIndex不会解除警报

Iphone UIAlertView didmissWithClickedButtonIndex不会解除警报,iphone,uialertview,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

当网络发生故障时,我将从处理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];
视图控制器:

- (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中保存一个引用并以这种方式将其忽略,就可以让它正常工作

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

alert = nil;

[alert dismissWithClickedButtonIndex:0 animated:YES];

你在零部分是正确的。我后来补充说,因为警报仍在显示,我在错误的地方补充说。即使没有它,它也不会使警报消失。因此,问题可能是您没有获得正确的“警报”,“viewWithTag”可能返回零,只需检查它,为什么不通过removeAlert中的[dict objectForKey:@“AlertView”]获取“警报”?也许您应该将“alert”设置为成员变量