2

プログラムの2つの異なる部分で、NSAlertから奇妙な動作が発生しています。動作は次のとおりです。

  1. アラートが表示された後、自然に消えます。
  2. アラートが再度表示され、ユーザーによって却下されるまで、つまり通常の動作が続くまで続きます。
  3. アラートが再び表示されます。

この動作は、アラートを表示するメソッドが初めて呼び出されたときにのみ発生します。その後、正常に動作します。

動作が発生する部分の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;

そうすれば、それは完璧に機能します。

4

5 に答える 5

2

最初にアラートを表示したときに、ロケーション マネージャーをオフにしていません。デバイスによって場所が精緻化される (つまり、精度が向上する) と、コールバックが (潜在的に) 複数回呼び出されます。アラートが表示されたら、[locMan stopUpdatingLocation] を使用する必要があります。

于 2010-02-16T13:53:57.433 に答える
1

いくつかの NSLog エントリを設定したところ、[locMan stopUpdatingLocation] を追加したにもかかわらず、didUpdateToLocation 関数が複数回実行されていることがわかりました。

アラートビューが再度呼び出され、プログラムが最初のインスタンスをクリアして2番目のインスタンスに自動的に道を譲るため、自然消滅が発生すると思います。

[locMan stopUpdatingLocation] が機能しない理由についてのアイデアをいただければ幸いですが、当面の間、locationAlert の宣言を関数から移動し (グローバルなので)、最初のlocate me 関数に設定して使用します最初に呼び出されたときは次のようになります。

[locationAlert show];
locationAlert = nil;

そうすれば完璧に動作します。

于 2010-02-16T19:13:26.257 に答える
0

アラートダイアログが一時的に表示され、再表示され、最後に閉じられた後に再び表示されるというまったく同じ問題が発生しました。アラート ビューを表示することを決定する前に、文字列の比較を行っていました。

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if([string isEqualToString:@"OK"]) {
    NSLog(@"(Settings)Registration Successful");
    statusField.text = @"Registration successful!";
    [settingsActivity stopAnimating];
}
else {
    NSLog(@"(Settings)Registration Failure");
    [settingsActivity stopAnimating];

    UIAlertView * regFail = [[[UIAlertView alloc] initWithTitle:@"Registration Error!" message:@"Please check your email address and try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil] autorelease];

    [regFail show];
}}

この動作を修正するために、アラートを表示するだけでなく、返された文字列を確認しました。

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if([string isEqualToString:@"OK"]) {
    NSLog(@"(Settings)Registration Successful");
    statusField.text = @"Registration successful!";
    [settingsActivity stopAnimating];
}
else if([string isEqualToString:@"Error"]) {
    NSLog(@"(Settings)Registration Failure");
    [settingsActivity stopAnimating];

    UIAlertView * regFail = [[[UIAlertView alloc] initWithTitle:@"Registration Error!" message:@"Please check your email address and try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil] autorelease];

    [regFail show];
}
于 2011-07-08T17:00:19.420 に答える
0

Location Manager の作業中にも同じ問題が発生しました。ここでNslogで確認しましたが、複数回実行されています。最終的に、複数のオブジェクトを作成し、ロケーションマネージャーを含む同じViewControllerにSharedinstanceを使用していることに気付きましたが、オブジェクトを解放していません。そのため、LocationManger で作業している間は、オブジェクトの処理を徹底的にチェックして、この種の問題を減らしてください。

于 2013-10-03T17:07:23.490 に答える