在我的程序的两个不同部分,我从 NSAlert 中得到了奇怪的行为。行为是:
- 警报出现,然后自发消失。
- 警报会再次出现,然后一直存在,直到被用户解除,即正常行为。
- 警报再次出现。
此行为仅在第一次调用显示警报的方法时发生。在第一次之后,它的行为正常。
这是发生行为的部分之一的代码:
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:@"You are in the right place." message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[locationAlert show];
[locationAlert release];
或者,如果您愿意,可以使用更多上下文:
- (IBAction)locateMe {
NSLog(@"About to check location");
locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
locMan.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
locMan.distanceFilter = 1609; //1 mile
[locMan startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation * )oldLocation {
if (newLocation.horizontalAccuracy >= 0) {
CLLocation *airportLocation = [[[CLLocation alloc] initWithLatitude:51.500148 longitude:-0.204669] autorelease];
CLLocationDistance delta = [airportLocation getDistanceFrom: newLocation];
long miles = (delta * 0.000621371) + 0.5; //metres to rounded mile
if (miles < 3) {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:@"You are in the right place." message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[locationAlert show];
[locationAlert release];
[locMan stopUpdatingLocation];
} else {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:@"You are not in the right place." message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[locationAlert show];
[locationAlert release];
[locMan stopUpdatingLocation];
}
}
}
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:@"Error." message:error.code delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[locationAlert show];
[locMan release];
locMan = nil;
}
有任何想法吗?谢谢。
编辑 - - - - -
发生这种情况的另一个地方是:
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to download feed from web site (Error code %i )", [parseError code]];
NSLog(@"error parsing XML: %@", errorString);
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
对于上下文,第一种情况在 AppDelegate 中,第二种情况在视图控制器中,用于第一个选项卡视图。第二个问题在没有互联网连接的情况下每次重新加载 xml 时都会出现。第一个仅在第一次调用该函数时发生。
编辑 - - -
如果我移动警报,它会起作用。不幸的是,这不是我想要的!
- (IBAction)locateMe {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:@"You are in the right place." message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[locationAlert show];
/*
NSLog(@"About to check location");
locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
locMan.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
locMan.distanceFilter = 1609; //1 mile
[locMan startUpdatingLocation];*/
}
更新:
我设置了一些 NSLog 条目,发现尽管添加[locMan stopUpdatingLocation]
了 didUpdateToLocation 函数,但它仍在运行多次。
我猜想自发消失是因为再次调用警报视图并且程序清除了第一个实例以自动为第二个实例让路。
关于为什么[locMan stopUpdatingLocation]
不起作用的任何想法将不胜感激,但同时我只是将 locationAlert 的声明移出函数(因此它是全局的),将其设置在初始的定位我函数中并使用以下第一个它被称为时间:
[locationAlert show];
locationAlert = nil;
这样它就可以完美地工作。