开发者

Extracting MKAnnotation Callout's Title When Sender

Ok guys, so I have a map view with annotations and when tapped, they display callouts with a disclosure icon on the right. When tapped, this function is called:

- (void)showDetails:(id)sender
{
    NSLog(@"showDetails: called!");
    NSLog(@"sender: %@",sender);
    PermitDetailViewController *permitDetail = [[PermitDetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
    NSLog(@"permitDetail.title: %@",perm开发者_开发知识库itDetail.title);
    permitDetail.title = sender.title; //compiler doesn't like this!
    NSLog(@"permitDetail.title: %@",permitDetail.title);
    [self.navigationController pushViewController:permitDetail animated:YES];
    [permitDetail release];
}

All well and good so far, but I need to know what the callout's title was. I am trying to do sender.title but that ain't working to well... Any ideas?

This is the console output when I change the problematic line to permitDetail.title = self.title;:

2010-12-02 11:50:06.044 Parking[55413:207] showDetails: called!
2010-12-02 11:50:06.045 Parking[55413:207] sender: <UIButton: 0x8139890; frame = (104 8; 29 31); opaque = NO; autoresize = LM; layer = <CALayer: 0x8139920>>
2010-12-02 11:50:06.045 Parking[55413:207] permitDetail.title: (null)
2010-12-02 11:50:06.045 Parking[55413:207] permitDetail.title: All Permits


The sender in your case is the callout button (not the MKAnnotation) so it doesn't have the title property.

In viewForAnnotation, remove the addTarget on the disclosure button. Just set the annotation view's rightCalloutAccessoryView to be the button.

Then implement the calloutAccessoryControlTapped delegate method which will be called when the callout is tapped. It also provides a reference to the annotation view in the call. The annotation view contains a reference to the annotation:

- (void)mapView:(MKMapView *)mapView 
        annotationView:(MKAnnotationView *)view 
        calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"callout annotation.title = %@", view.annotation.title);

    //do your show details thing here...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜