【问题标题】:Extracting MKAnnotation Callout's Title When Sender发送者时提取 MKAnnotation 标注的标题
【发布时间】:2010-12-02 20:19:18
【问题描述】:

好的,伙计们,我有一个带有注释的地图视图,当点击它们时,它们会在右侧显示带有披露图标的标注。点击时,调用此函数:

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

到目前为止一切都很好,但我需要知道标注的标题是什么。我正在尝试做 sender.title 但这不是很好......有什么想法吗?

这是我将有问题的行更改为 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

【问题讨论】:

    标签: iphone ios mkannotation callouts


    【解决方案1】:

    在您的情况下,发件人是标注按钮(不是 MKAnnotation),因此它没有标题属性。

    在 viewForAnnotation 中,移除公开按钮上的 addTarget。只需将注释视图的 rightCalloutAccessoryView 设置为按钮即可。

    然后实现 calloutAccessoryControlTapped 委托方法,该方法将在点击标注时调用。它还提供了对调用中注释视图的引用。注释视图包含对注释的引用:

    - (void)mapView:(MKMapView *)mapView 
            annotationView:(MKAnnotationView *)view 
            calloutAccessoryControlTapped:(UIControl *)control
    {
        NSLog(@"callout annotation.title = %@", view.annotation.title);
    
        //do your show details thing here...
    }
    

    【讨论】:

    • 我被这句话弄糊涂了:“在viewForAnnotation中,去掉disclosure button上的addTarget。只要把annotation view的rightCalloutAccessoryView设置为button即可。”在我看来,您是在建议我去掉标注上的披露图标,只保留它,以便用户点击标注以进入详细视图。如果可能的话,我真的更希望在那里有那个披露图标。我还是觉得我不太明白你说的一切,介意给我解释一下吗?谢谢。
    • 没关系...我想通了。我摆脱了 addButton... 方法: UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; annotationView.rightCalloutAccessoryView = rightButton;然后我实施了你所说的实施,它奏效了!你知道你的东西老兄,道具!
    • 谢谢,很高兴它成功了。对不起,我不清楚。我的意思是删除对 addTarget 的调用。您无需指定自己的方法来调用标注点击,因为地图视图有一个委托。
    • 是的,我试过之后才知道这就是你的意思,它奏效了,哈哈。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多