我有一个带有引脚注释的地图视图。当用户按下引脚信息的显示按钮时,我需要将引脚的坐标传递给详细视图。当我进入showDetails方法时,如何获取引脚的坐标?我正在使用下一个代码。
- (void)showDetails:(id)sender {
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil ];
// HERE I NEED TO PASS THE COORDINATES OF THE PIN TO THE DETAILVIEWCONTROLLER.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
else { // Handles the other annotations.
// Try to dequeue an existing pin view first.
static NSString *AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView) {
// If an existing pin view was not available, creates one.
MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
// Adds a detail disclosure button.
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
} else
pinView.annotation = annotation;
}
return nil;
}
感谢阅读。
答案 0 :(得分:0)
您可以按照以下方式自定义UIButton按钮:
@interface CustomButton : UIButton {
CLLocationCoordinate2D pinCoordinate; }
现在在你的viewForAnnotation中:
// Adds a detail disclosure button.
CustomButton *rightButton = [CustomButton buttonWithType:UIButtonTypeDetailDisclosure];
rightButton.pinCoordinate = annotation.coordinate
[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;
最后得到showDetails方法中的坐标:
CLLocationCoordinate2D currentCoord = ((CustomButton*)sender).pinCoordinate;
答案 1 :(得分:0)
我现在不能给你一个有效的代码解决方案,因为我这里没有我的mac。但我可以告诉你一个可能的方法。
在DetailViewController中创建一个填充其属性的方法,以便在推送之前从showDetails中调用它。所以事件的顺序就是这样。
点击披露按钮 - &gt; showDetails调用 - &gt; DetailViewController创建并填充 - &gt;推DetailViewController
希望这有助于你