显示出另一种观点被点击地图标注时 [英] show another view when map annotation are clicked
问题描述
我只有一个annotation.I地图创建了一个简单类,我希望它当用户点击annotation.The问题表明的是,当我点击注解没有任何反应。
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.
下面是我的code:
- (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
函数永远不会被called.Please帮助me.I'm新的iPhone和我会忘记一个简单的thing.Thanks
My showDetailView
function never gets called.Please help me.I'm new to iphone and I might forget a simple thing.Thanks
仍然没有工作!!!!
推荐答案
首先,检查地图视图的委托
设置,否则你的 viewForAnnotation
方法将不会被调用。
First, check that the map view's delegate
is set otherwise your viewForAnnotation
method will not get called.
接下来,附件按钮出现在标注的的标注的如果你设置这只会出现 canShowCallout
:
Next, the accessory button appears on the annotation's callout which will only appear if you set canShowCallout
:
annView.canShowCallout = YES;
其次,而不是用自己的方法来处理按钮的动作,这是最好使用窃听附件时,它被调用地图视图自己的 calloutAccessoryControlTapped
的委托方法:
- (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 ...
行。
另外请注意你的的NSLog
在showDetailView方法缺少龙头 @
,这将导致崩溃的时候方法不会被调用。
Also note your NSLog
in the showDetailView method is missing the leading @
which will result in a crash when the method does get called.
另一件事是,你应该使用 dequeueReusableAnnotationViewWithIdentifier
在 viewForAnnotation
启用注解视图重新使用。
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屋!