在我的应用程序中,有一个 mapView,用户可以通过按下屏幕来添加注释。当注释被拖放到 mapView 上时,它们会被添加到单例中的可变数组中。注释视图有一个标注按钮,该按钮在按下时将详细视图(称为 PinViewController)推送到堆栈上。我的问题是,当按下标注按钮时,我想将该注释对象的索引传递给详细视图控制器,以便我可以从该对象获取地址和日期。但是每当我按下标注按钮时,无论它是哪个注释,它总是传递数组中第一个对象的索引。我看不出我做错了什么,有人可以帮我吗?这是一些代码:
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control {
PinViewController *pvc = [[PinViewController alloc]init];
[[self navigationController]pushViewController:pvc animated:YES];
NSUInteger pinIndex = [[Data singleton].annotations indexOfObject:view.annotation];
[pvc setIdentifier:pinIndex];
}
这是来自详细视图控制器:
- (void)viewDidLoad {
[super viewDidLoad];
[self.labelView setFrame:CGRectMake(10, 260, 300, 230)];
[PinViewController roundView:self.labelView onCorner:UIRectCornerAllCorners radius:15];
[[self view]addSubview:self.labelView];
self.annotation = [[Data singleton].annotations objectAtIndex:self.identifier];
self.addressLabel.text = self.annotation.address;
self.dateLabel.text = self.annotation.subtitle;
}
这是将注释添加到数组的位置:
- (void)press:(UILongPressGestureRecognizer *)recognizer {
CGPoint touchPoint = [recognizer locationInView:self.worldView];
CLLocationCoordinate2D touchMapCoordinate = [self.worldView convertPoint:touchPoint toCoordinateFromView:self.worldView];
self.geocoder = [[CLGeocoder alloc]init];
CLLocation *location = [[CLLocation alloc]initWithCoordinate:touchMapCoordinate
altitude:CLLocationDistanceMax
horizontalAccuracy:kCLLocationAccuracyBest
verticalAccuracy:kCLLocationAccuracyBest
timestamp:[NSDate date]];
[self.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];
self.geocodedAddress = [NSString stringWithFormat:@"%@ %@, %@ %@", [place subThoroughfare], [place thoroughfare], [place locality], [place administrativeArea]];
if (UIGestureRecognizerStateBegan == [recognizer state]) {
self.addressPin = [[MapPoint alloc]initWithAddress:self.geocodedAddress
coordinate:touchMapCoordinate
title:self.geocodedAddress];
[[Data singleton].annotations addObject:self.addressPin];
[self.worldView addAnnotation:self.addressPin];
NSLog(@"The number of pins in the annotation array is: %u",[Data singleton].annotations.count);
}
}
}];
}
所以基本上,传递给 PinViewController 的索引总是 0,不管它是哪个注解。我不明白为什么这段代码不起作用。