当位置管理器找不到当前位置时,我正在显示alertViews。我是这样做的

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {

[manager stopUpdatingLocation];

switch([error code])
{
    case kCLErrorNetwork: // general, network-related error
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please check your network connection or that you are not in airplane mode" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
        break;
    case kCLErrorDenied:{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"User has denied to use current Location " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
        break;
    default:
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unknown network error" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
        break;
}

}


我的问题是locationManager didFailWithError方法被反复调用。因此,我的alertviews反复显示。

我该如何解决这个问题?

最佳答案

对于正在进行的后台进程(例如位置管理器)可能触发的错误消息,一个好的开始是存储对UIAlertView的引用。当委托收到alertView:clickedButtonAtIndex:时,将此引用重置为nil。在显示新错误之前,请检查是否已经可见。

更好的是,不要显示来自后台进程的模式错误消息。只是四处走动,您偶尔会失去接纳感,而在发生这种情况时必须消除错误很繁琐。如果您的应用程序具有配置或帮助屏幕,请在其中添加一个空格,以存放来自位置管理器(或游戏中心或iCloud或其他偶尔失去连接性的信息)的最新错误消息。

10-01 16:18