mapViewDidFailLoadingMap中的UIAlertView:withError:

时间:2011-05-02 11:30:46

标签: ios mkmapview uialertview

我将通过处理加载MapView的错误来改进我的iOS程序。如果没有互联网连接,我会显示警告。但是方法 mapViewDidFailLoadingMap 会一次又一次地调用UIAlert。 我怎样才能实现警报只显示一次?

- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error {
NSString *domain = [error domain];
NSInteger code = [error code];
NSLog(@"userInfo: %@", [error userInfo]);
NSLog(@"localizedDescription: %@", [error localizedDescription]);
NSLog(@"localizedFailureReason: %@", [error localizedFailureReason]);
NSLog(@"localizedRecoverySuggestion: %@", [error localizedRecoverySuggestion]);
NSLog(@"localizedRecoveryOptions: %@", [[error localizedRecoveryOptions] description]);

if ([domain isEqualToString:NSURLErrorDomain]) {
    if (code == NSURLErrorNotConnectedToInternet) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Map Loading Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release]; 
    }
}

}

2 个答案:

答案 0 :(得分:0)

如果您的程序逻辑允许,那么您可以在发生故障/错误时将MKMapView对象的委托设置为nil,并且它将停止向委托发送失败/错误消息,从而阻止显示多个警报。

您可以在mapViewDidFailLoadingMap:withError方法中将委托设置为nil。

答案 1 :(得分:0)

您可以使用BOOL标记来确保错误仅针对MKMapView显示一次:

- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
    if (displayErrors) {
        // display the error...
        displayErrors = NO; 
    }
}

如果您重置相应视图控制器的viewWillAppear中的标志,则每次访问视图时都会显示一次错误:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    displayErrors = YES;
}