如何在 iPhone 中单击按钮时调用 pin 注释
我不希望直接在地图上调用图钉,我希望按钮操作调用图钉注释。
当我在按钮单击事件上调用此方法时,我的应用程序崩溃了。我想在单击按钮时调用注释。我可以在按钮上调用所有方法吗?
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier: @"restMap"];
if (pin == nil) {
pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"restMap"] autorelease];
} else {
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorRed
pin.canShowCallout = YES;
pin.animatesDrop = NO;
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
NSInteger annotationValue = [self.annotations indexOfObject:annotation];
detailButton.tag = annotationValue;
[detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
pin.rightCalloutAccessoryView = detailButton;
return pin;
}
I dont want pin to be call directly on map, I want this on button action to call pin annotation.
When I call this method on button click event my app was crash. I want to call annotation on button click. Can I call this all method on button?
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier: @"restMap"];
if (pin == nil) {
pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"restMap"] autorelease];
} else {
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorRed
pin.canShowCallout = YES;
pin.animatesDrop = NO;
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
NSInteger annotationValue = [self.annotations indexOfObject:annotation];
detailButton.tag = annotationValue;
[detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
pin.rightCalloutAccessoryView = detailButton;
return pin;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试调用的方法是 MapView 委托方法,并且只能由您的 MapView 调用。此方法仅用于在 mapView 需要时“按需”提供注释的视图。即当注释位于或即将移入mapView 的可见区域时。
您需要将注释对象添加到您的地图视图中。该对象必须符合 MKAnnotation 协议。
http://developer.apple.com/library/ios/documentation/ MapKit/Reference/MKAnnotation_Protocol/
您将通过在按下按钮时调用的 IBAction 方法中调用 [yourMapView addAnnotation: yourAnnotationObject] 将对象添加到 mapView 中。
The method that you are trying to call is a mapView delegate method and should only be called by your MapView. This method is only for providing the annotation's view "on demand" when the mapView needs it. I.e. When the annotation is in or about to move into the mapView's visible region.
You need to add an annotation object to your mapView. This object must conform to the MKAnnotation Protocol.
http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKAnnotation_Protocol/
You will add the object to the mapView by calling [yourMapView addAnnotation: yourAnnotationObject] in the IBAction method that is called when your button is pushed.