我在程序的两个不同部分中从 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];*/
}
Update:
我设置了一些 NSLog 条目并发现尽管添加了[locMan stopUpdatingLocation]
didUpdateToLocation 函数运行了多次。
我猜想自发消失的发生是因为再次调用警报视图并且程序自动清除第一个实例为第二个实例让路。
关于为什么的任何想法[locMan stopUpdatingLocation]
不起作用将不胜感激,但同时我只是将 locationAlert 的声明移出函数(因此它是全局的),将其设置在初始的“定位我”函数中,并在第一次调用时使用以下内容:
[locationAlert show];
locationAlert = nil;
这样它就完美地工作了。