我有一个只有一个注释的地图。我创建了一个简单的类,我希望它在用户点击注释时显示。问题是当我点击注释时没有任何反应。
这是我的代码:
- (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的新手,我可能会忘记一件简单的事情。谢谢
仍然没有工作!!!!
答案 0 :(得分:16)
首先,检查地图视图的delegate
是否已设置,否则将无法调用viewForAnnotation
方法。
接下来,附件按钮会出现在注释的标注上,仅当您设置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
}
从[detailButton addTarget...
删除viewForAnnotation
行。
另请注意,showDetailView方法中的NSLog
缺少前导@
,这会在调用方法时导致崩溃。
另一件事是你应该在dequeueReusableAnnotationViewWithIdentifier
中使用viewForAnnotation
来重复使用注释视图。
请求示例:
- (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];
}
答案 1 :(得分:2)
确定。如果您使用的是故事板,则需要在地图视图控制器之间创建一个segue,然后在下面的代码所在的位置创建一个segue:
- (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];
}
请改用以下代码:
NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
[self performSegueWithIdentifier:@"DetailViewController" sender:self];
这将为您提供详细视图的移动。
答案 2 :(得分:0)
像这样编写你的函数:
-(IBAction)showDetailView:(id)sender{
NSLog("inside the stupid method");
MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
答案 3 :(得分:0)
将-(IBAction)showDetailView:(UIView*)sender
替换为以下内容......
-(void)showDetailView:(id)sender
答案 4 :(得分:0)
你必须像这样将地图委托设置为自己,
mapview.delegate=self;
(如果您在同一页面中调用该方法)
如果您没有在代码中分配代理,则不会调用相应的方法,因此请检查您是否具有上述行。