2012-06-22 61 views
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); 

     } 
} 

...但问题是,当我抱着,然后拖动添加多个引脚。我只想添加一个引脚。所以我尝试了do方法,但它不起作用。我无法理解,因为当我执行代码时,我将NSNumber的值变为1,而while说= 0来运行代码。

请帮忙!!

回答

0

您目前的代码容易出现大量内存泄漏。例如:

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

正在泄漏,因为你离开的existingpoints与一审保留1值,而不是在任何地方释放它。除非你使用ARC。你可以只用一个指令优化上述代码:

NSNumber* existingpoints = [NSNumber numberWithInt:0]; 

如果你需要保持它的地方保留它(但我相信的是不是这样)。

分析代码,我建议不要使用现有的点作为NSNumber。改为使用NSInteger(这不是一个对象,只是long的typedef)。

这里是我的改写代码:

-(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时创建任何内存泄漏创建latitudelongitude

编辑: 进一步分析你的代码,我不明白为什么这种方法会一次下降两个引脚。也许你可以检查你的方法是否被两次调用?

更多:如果你只是想让它运行一次,为什么你有一个do/while循环? (但也许你只是铺平了道路,以进一步提前)

+0

我使用ARC .. – Alessandro

+0

除了[[annotationPoint release];'部分,代码仍然应该在ARC上运行良好。正如我所说,你应该检查方法是否被调用两次。 –

+0

我用这个函数调用它:UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture :)]; [self.mapView addGestureRecognizer:longPressGesture]; – Alessandro