2012-10-11 61 views
1

我正在创建一个MKAnnotationView与详细信息泄露按钮。UIButton无法正确加载MKAnnotationView

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

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

在图形页面: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,直到你点击它并出现。

严重的是我遇到的更奇怪的问题之一。

回答

2

在调用didSelectAnnotationView委托方法之前,地图视图已经基于注解视图的属性(在更改之前)准备了标注视图。

因此,您在第一次点击时看到的标注没有应用程序在didSelectAnnotationView中所做的更改。在以下的水龙头中,标注可以基于从先前水龙头设置的值(这实际上取决于在viewForAnnotation中如何处理注解视图的重用)。

它看起来像代码在didSelectAnnotationViewbuttonForAnnotation中唯一做的事情是设置按钮动作和标记。

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

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

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

在第一个选项,做viewForAnnotationaddTarget和不打扰设置tag。您也不需要buttonForAnnotation方法。然后在按钮操作方法中,从mapView.selectedAnnotations获取选定的注释。

当前,您的操作方法在self.delegate上,因此您可能在访问来自其他控制器的地图视图时遇到一些问题。你可以做的是创建地图控制器本地按钮操作方法它获取所选择的注释和然后呼吁self.delegatepresentAnnotationPhoto:操作方法(除了现在可以写这个方法接受一个注释参数而不是一个按钮点击处理程序)。除非你不需要做任何addTarget并在calloutAccessoryControlTapped方法,调用presentAnnotationPhoto:self.delegate

第二个选项是相似的。

对于这两个选项中,我建议修改的当前UIButton *和地图控制器代替presentAnnotationPhoto:方法接受注释对象本身(FlickrPhotoAnnotation *),做一个addTarget上的方法本地到地图控制器(或使用calloutAccessoryControlTapped)并从该方法中手动调用presentAnnotationPhoto:并将其传递给注释。

+0

如果你在我面前,我会吻你。非常感谢你抽出时间帮助我解决这个问题。 另外,两年前,我去看了安娜卡列尼娜,并喜欢它。现在我已经说过了,我希望你没有选择那个用户名,因为你认为这个节目很荒谬,哈哈。 再次感谢。 –