プログラムの2つの異なる部分で、NSAlertから奇妙な動作が発生しています。動作は次のとおりです。
- アラートが表示された後、自然に消えます。
- アラートが再度表示され、ユーザーによって却下されるまで、つまり通常の動作が続くまで続きます。
- アラートが再び表示されます。
この動作は、アラートを表示するメソッドが初めて呼び出されたときにのみ発生します。その後、正常に動作します。
動作が発生する部分の1つのコードは次のとおりです。
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にあり、2番目のケースは1番目のタブビューのビューコントローラーにあります。2番目の問題は、インターネットに接続されていないときに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関数を追加したにもかかわらず、複数回実行されていることがわかりました。
アラートビューが再度呼び出され、プログラムが最初のインスタンスをクリアして2番目のインスタンスに自動的に道を譲るために、自発的な消失が発生すると思います。
なぜ機能しないのかについてのアイデアは[locMan stopUpdatingLocation]
ありがたいですが、その間にlocationAlertの宣言を関数から移動し(グローバルであるため)、最初のlocate me関数に設定し、次の最初の関数を使用しますそれが呼ばれる時間:
[locationAlert show];
locationAlert = nil;
そうすれば、それは完璧に機能します。