我有一张只有一个注释的 map 。我创建了一个简单的类,我希望它在用户单击注释时显示。问题是,当我单击注释时什么也没有发生。
这是我的代码:
- (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 新手,我可能会忘记一件简单的事情。谢谢
还是不行!!!
最佳答案
首先,检查 map View 的 delegate
是否已设置,否则您的 viewForAnnotation
方法将不会被调用。
接下来,附件按钮将出现在注释的标注上,仅当您设置canShowCallout
时才会出现:
annView.canShowCallout = YES;
接下来,不要使用您自己的方法来处理按钮操作,最好使用 map View 自己的 calloutAccessoryControlTapped
委托(delegate)方法,该方法在点击附件时会被调用:
- (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
缺少前导 @
,这将导致调用该方法时崩溃。
另一件事是,您应该在 viewForAnnotation
中使用 dequeueReusableAnnotationViewWithIdentifier
来启用注释 View 重用。
请求的示例:
- (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];
}
关于iphone - 单击 map 注释时显示另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7334068/