0

私はこのコードを持っています:

-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {

NSNumber* existingpoints = [[NSNumber alloc]init];


 existingpoints =[NSNumber numberWithInt:0]; 


// This is important if you only want to receive one tap and hold event
if (sender.state == UIGestureRecognizerStateEnded)
{
    [self.mapView removeGestureRecognizer:sender];
}
else {

    do {
        int z = 1;
        existingpoints =[NSNumber numberWithInt:z];

        // Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
        CGPoint point = [sender locationInView:self.mapView];
        CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
        // Then all you have to do is create the annotation and add it to the map

        MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init]; annotationPoint.coordinate = locCoord;



        NSString *latitude = [[NSString alloc] initWithFormat:@"%f",locCoord.latitude];


        NSString *longitude = [[NSString alloc] initWithFormat:@"%f", locCoord.longitude];


        annotationPoint.title = @"Event";
        annotationPoint.subtitle = [NSString stringWithFormat:@"%@ & %@", latitude, longitude];

        [mapView addAnnotation:annotationPoint];


        [[NSUserDefaults standardUserDefaults]setObject:latitude forKey:@"FolderLatitude"];
        [[NSUserDefaults standardUserDefaults]setObject:longitude forKey:@"FolderLongitude"];


    } while ([existingpoints intValue] == 0);

        }
}

...しかし、問題は、押したままドラッグすると、複数のピンが追加されることです。ピンを1つだけ追加したい。だから私はdoメソッドを試しましたが、うまくいきません。コードを実行したときに NSNumber の値を 1 にすると、while は = 0 と言ってコードを実行するため、理解できません。

助けてください!!

4

1 に答える 1

0

現在のコードでは、かなりの数のメモリ リークが発生する傾向があります。例えば:

NSNumber* existingpoints = [[NSNumber alloc] init];
existingpoints = [NSNumber numberWithInt:0];

の最初のインスタンスexistingpointsを保持値 1 のままにし、どこにも解放しないため、リークしています。ARCを使用していない限り。たった 1 つの命令で上記のコードを最適化できます。

NSNumber* existingpoints = [NSNumber numberWithInt:0];

そして、どこかに保管する必要がある場合は保管してください(しかし、そうではないと思います)。

コードを分析すると、既存のポイントを として使用しないことをお勧めしますNSNumber。代わりにを使用しNSIntegerます (これはオブジェクトではなく、単なる typedeflongです)。

書き直したコードは次のとおりです。

-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {
    NSInteger existingpoints = 0;

    // This is important if you only want to receive one tap and hold event
    if (sender.state == UIGestureRecognizerStateEnded) {
        [self.mapView removeGestureRecognizer:sender];
    }
    else {
        do {
            int z = 1;
            existingpoints = z;

            // Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
            CGPoint point = [sender locationInView:self.mapView];
            CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];

            // Then all you have to do is create the annotation and add it to the map
            MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
            annotationPoint.coordinate = locCoord;

            NSString *latitude = [NSString stringWithFormat:@"%f",locCoord.latitude];
            NSString *longitude = [NSString stringWithFormat:@"%f", locCoord.longitude];

            annotationPoint.title = @"Event";
            annotationPoint.subtitle = [NSString stringWithFormat:@"%@ & %@", latitude, longitude];

            [mapView addAnnotation:annotationPoint];

            [[NSUserDefaults standardUserDefaults] setObject:latitude forKey:@"FolderLatitude"];
            [[NSUserDefaults standardUserDefaults] setObject:longitude forKey:@"FolderLongitude"];

            [annotationPoint release]; // Remove this if you're using ARC.
        } while (existingpoints == 0);
    }
}

ARC 使用時にメモリ リークを作成するコードと作成しないlatitudeコードも変更したことに注意してください。longitude

編集: コードをさらに分析すると、このメソッドが一度に 2 つのピンをドロップする理由がわかりません。メソッドが 2 回呼び出されていないかどうかを確認できますか?

詳細: 一度だけ実行したいのに、なぜ do/while ループがあるのですか? (しかし、おそらくあなたはさらに先へと足を踏み入れているだけです)

于 2012-06-22T17:28:11.033 に答える