单击地图注释时显示另一个视图 [英] show another view when map annotation are clicked
问题描述
我有一张只有一个注释的地图.我创建了一个简单的类,我希望它在用户单击注释时显示.问题是当我单击注释时什么也没有发生.
I have a map with only one annotation.I created a simple class which I want it to show when the user clicks on the annotation.The problem is that when I click on the annotation nothing happens.
这是我的代码:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
NSLog(@"Reverse Geocoder completed");
mPlacemark=placemark;
[mapView addAnnotation:placemark];
}
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.animatesDrop=TRUE;
//create UIButton for annotation
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//NSInteger annotationValue = [self.annotations indexOfObject:annotation];
[detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView=detailButton;
return annView;
}
-(void)showDetailView:(id)sender{
NSLog("inside the stupid method");
MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
我的 showDetailView
函数永远不会被调用.请帮助我.我是 iphone 新手,我可能会忘记一件简单的事情.谢谢
My showDetailView
function never gets called.Please help me.I'm new to iphone and I might forget a simple thing.Thanks
还是不行!!!!
推荐答案
首先,检查地图视图的 delegate
是否设置,否则您的 viewForAnnotation
方法将不会被调用.
First, check that the map view's delegate
is set otherwise your viewForAnnotation
method will not get called.
接下来,附件按钮出现在注解的callout上,只有在你设置了canShowCallout
时才会出现:
Next, the accessory button appears on the annotation's callout which will only appear if you set canShowCallout
:
annView.canShowCallout = YES;
接下来,与其使用您自己的方法来处理按钮操作,不如使用地图视图自己的 calloutAccessoryControlTapped
委托方法,该方法在点击附件时会被调用:
Next, instead of using your own method to handle the button action, it's much better to use the map view's own calloutAccessoryControlTapped
delegate method which gets called when tapping an accessory:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"inside the stupid method");
//Here, the annotation tapped can be accessed using view.annotation
}
从 viewForAnnotation
中删除 [detailButton addTarget...
行.
还要注意 showDetailView 方法中的 NSLog
缺少前导 @
,这将导致调用该方法时崩溃.
Also note your NSLog
in the showDetailView method is missing the leading @
which will result in a crash when the method does get called.
另一件事是你应该在 viewForAnnotation
中使用 dequeueReusableAnnotationViewWithIdentifier
来启用注释视图的重用.
Another thing is you should use dequeueReusableAnnotationViewWithIdentifier
in viewForAnnotation
to enable annotation view re-use.
要求的示例:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
static NSString *annReuseId = @"currentloc";
MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annReuseId];
if (annView == nil)
{
annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annReuseId];
annView.animatesDrop = YES;
annView.canShowCallout = YES;
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.rightCalloutAccessoryView=detailButton;
}
else {
annView.annotation = annotation;
}
return annView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
//here, can set annotation info in some property of detailView
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
这篇关于单击地图注释时显示另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!