将注释标题传递给新视图

时间:2012-10-29 00:57:25

标签: objective-c annotations

在我的应用程序中,我有一个使用UILongPressGestureRecognizer删除图钉的mapView。按下屏幕时,对坐标进行地理编码并删除引脚,并将地址设置为标题。屏幕上会有多个引脚,注释视图有一个标注附件,可以将另一个名为PinViewController的控制器推入堆栈。 PinViewController有一个标签,我想要显示引脚的标题。

以下是删除引脚的代码:

-(void)press:(UILongPressGestureRecognizer *)recognizer
{
    CGPoint touchPoint = [recognizer locationInView:worldView];
    CLLocationCoordinate2D touchMapCoordinate = [worldView convertPoint:touchPoint toCoordinateFromView:worldView];

    geocoder = [[CLGeocoder alloc]init];
    CLLocation *location = [[CLLocation alloc]initWithCoordinate:touchMapCoordinate
                                                    altitude:CLLocationDistanceMax
                                          horizontalAccuracy:kCLLocationAccuracyBest
                                            verticalAccuracy:kCLLocationAccuracyBest
                                                   timestamp:[NSDate date]];
    [geocoder reverseGeocodeLocation:location
               completionHandler:^(NSArray *placemarks, NSError *error) {
                   NSLog(@"reverseGeocoder:completionHandler: called");
                   if (error) {
                       NSLog(@"Geocoder failed with error: %@", error);
                   } else {
                       CLPlacemark *place = [placemarks objectAtIndex:0];
                       geocodedAddress = [NSString stringWithFormat:@"%@ %@, %@ %@", [place subThoroughfare], [place thoroughfare], [place locality], [place administrativeArea]];

                       if (UIGestureRecognizerStateBegan == [recognizer state]) {
                           addressPin = [[MapPoint alloc]initWithAddress:geocodedAddress coordinate:touchMapCoordinate
                                                                   title:geocodedAddress];
                           [worldView addAnnotation:addressPin];
                       }
                   }
               }];
}

以下是将PinViewController推送到堆栈的代码:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    PinViewController *pinViewController = [[PinViewController alloc]init];
    [[self navigationController]pushViewController:pinViewController animated:YES];
    pinViewController.label.text = addressPin.title;
}

我遇到的麻烦是标签只显示要进行地理编码的最后一个地址。因此,当我放下一个引脚并按下标注附件按钮时,正确的地址将被推送到PinViewController。但是如果我按下另一个注释的标注附件按钮,则最后一个引脚的地址被推送到PinViewController。所以我需要找到一种方法,以便当我按下注释视图的标注附件按钮时,注释的标题将传递给PinViewController。我真的很感激一些帮助。

1 个答案:

答案 0 :(得分:2)

您的问题是您正在使用addressPin,它始终是地理编码的最后一个引脚。您需要使用annotationView来访问已按下的注释。

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    PinViewController *pinViewController = [[PinViewController alloc]init];
    [[self navigationController]pushViewController:pinViewController animated:YES];
    pinViewController.label.text = view.annotation.title;
}