点击userLocation的标题

时间:2015-02-17 15:51:11

标签: ios mapkit mkannotationview

我想点击userLocation标题添加操作。所以我尝试添加rightCalloutAccessoryView但是想要保留默认标记。 我怎么能这样做?

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

    if([annotation isKindOfClass:[MKUserLocation class]]){
        MKAnnotationView *annotationView = //What need I put here?
        annotationView.canShowCallout = YES;
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];

    return annotationView;
}else {
    return nil;
}
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"DetailsIphone" sender:view];
}

1 个答案:

答案 0 :(得分:1)

使用:

   - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
      NSLog(@" we selected annotation");
      //self.mapView is your mapView ->
     if(view.annotation == self.mapView.userLocation)
     {
       // do your action here like [self method]; in your case->
     [self performSegueWithIdentifier:@"DetailsIphone" sender:view];  
     } 
       else {
     //no userlocation annotation was tapped ...another code  
     }
  }

更新回答:

   - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
  NSLog(@" we selected annotation");
  //self.mapView is your mapView ->
 if(view.annotation == self.mapView.userLocation)
 {
   // do your action here like [self method]; in your case->
   UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(calloutTapped:)];
[view addGestureRecognizer:tapGesture];
 } 
   else {
   //no userlocation annotation was tapped ...another code  
   }
}


   -(void)calloutTapped:(UITapGestureRecognizer *) sender
{
    NSLog(@"Callout was tapped");
   [self performSegueWithIdentifier:@"DetailsIphone" sender:view];
 }