【问题标题】:UIButton Not Loading Properly For MKAnnotationViewUIButton 没有为 MKAnnotationView 正确加载
【发布时间】:2012-10-11 08:35:05
【问题描述】:

我正在创建一个带有详细信息披露按钮的 MKAnnotationView。

在mapView:viewForAnnotation:我只是创建了一个占位符按钮。

//  the right accessory view needs to be a disclosure button ready to bring up the photo
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

在mapView:didSelectAnnotationView:我实际上创建了一个按钮来使用(带有相关标签)

//  create a button for the callout
UIButton *disclosure                = [self.delegate mapController:self buttonForAnnotation:aView.annotation];

NSLog(@"DisclosureButton: %@", disclosure);

//  set the button's target for when it is tapped upon
[disclosure addTarget:self.delegate action:@selector(presentAnnotationPhoto:) forControlEvents:UIControlEventTouchUpInside];

//  make the button the right callout accessory view
aView.rightCalloutAccessoryView = disclosure;

在日志中,按钮似乎已完全实例化,并且设置了正确的标签。

这是按钮创建者:

/**
 *  returns an button for a specific annotation
 *
 *  @param  sender              the map controller which is sending this method to us (its' delegate)
 *  @param  annotation          the annotation we need to create a button for
 */
- (UIButton *)mapController:(MapController *)   sender
        buttonForAnnotation:(id <MKAnnotation>) annotation
{
    //  get the annotation as a flickr photo annotation
    FlickrPhotoAnnotation *fpa  = (FlickrPhotoAnnotation *)annotation;

    //  create a disclosure button used for showing photo in callout
    UIButton *disclosureButton      = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    //  associate the correct photo with the button
    disclosureButton.tag            = [self.photoList indexOfObject:fpa.photo];

    return disclosureButton;
}

当我选择注释时出现问题。在选择注释并点击详细信息披露按钮的几秒钟内,没有任何反应。但是,在点击并返回注释几次并测试按钮后,它最终可以按预期工作。

奇怪的延迟是怎么回事?有时当按钮开始工作时,它只是看起来好像 alpha 设置为 0.0,直到您点击它并出现。

这是我遇到的最奇怪的问题之一。

【问题讨论】:

    标签: objective-c uibutton mkannotationview


    【解决方案1】:

    在调用didSelectAnnotationView委托方法之前,地图视图已经根据注释视图的属性准备了标注视图(在您进行更改之前)。

    因此,您在第一次点击时看到的标注没有应用在 didSelectAnnotationView 中所做的更改。在接下来的点击中,标注可能基于上一次点击设置的值(这实际上取决于viewForAnnotation 中如何处理注释视图重用)。

    看起来代码在didSelectAnnotationViewbuttonForAnnotation 中所做的唯一事情就是设置按钮操作和标签。

    我假设您使用的是“标记”方法,因为 presentAnnotationPhoto: 方法需要引用所选注释的属性。

    您不需要使用标签来获取您的操作方法中的选定注释。相反,有几个更好的选择:

    • 您的自定义操作方法可以从地图视图的selectedAnnotations 属性中获取选定的注释。有关如何执行此操作的示例,请参阅 this question
    • 使用地图视图自己的委托方法calloutAccessoryControlTapped,而不是自定义操作方法。委托方法传递对注释视图的引用,该视图包含指向其注释的属性(即view.annotation),因此无需猜测、搜索或询问选择了哪个注释。我推荐这个选项。

    在第一个选项中,在viewForAnnotation 中执行addTarget,不要费心设置tag。您也不需要buttonForAnnotation 方法。然后在按钮动作方法中,从mapView.selectedAnnotations获取选中的注解。

    目前,您的操作方法在 self.delegate 上,因此您可能无法从其他控制器访问地图视图。您可以做的是在地图控制器中创建一个本地按钮操作方法,该方法获取选定的注释并然后调用presentAnnotationPhoto: 操作方法在self.delegate 上(除了现在可以编写该方法以接受注释参数,而不是作为按钮点击处理程序)。

    第二个选项类似,除了你不需要做任何addTarget 并且在calloutAccessoryControlTapped 方法中,在self.delegate 上调用presentAnnotationPhoto:

    对于这两个选项,我建议修改presentAnnotationPhoto: 方法以接受注释对象本身(FlickrPhotoAnnotation *)而不是当前的UIButton *,并在地图控制器中,对方法执行addTarget local 到地图控制器(或使用 calloutAccessoryControlTapped),然后从该方法手动调用 presentAnnotationPhoto: 并将注释传递给它。

    【讨论】:

    • 如果你在我面前,我会亲你。非常感谢您抽出宝贵时间帮助我深入解决这个问题。顺便说一句,两年前我去看了安娜卡列尼娜,很喜欢。既然我已经说了,我希望你没有选择那个用户名,因为你认为这个节目很荒谬,哈哈。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2015-12-20
    相关资源
    最近更新 更多