Ios 如何将按钮添加到MapKit详图索引Swift 3
我正在尝试向详图索引添加一个按钮,以便可以过渡到另一个视图控制器。然而,标注不会显示附件视图,我不确定原因。我从firebase中获取一些数据并将其输入到适当的数组中,然后在调用时发布注释。我不知道为什么按钮没有显示出来 此代码发生在ViewDidLoad()中{…} 我也使用了这个函数Ios 如何将按钮添加到MapKit详图索引Swift 3,ios,swift,firebase,mapkit,Ios,Swift,Firebase,Mapkit,我正在尝试向详图索引添加一个按钮,以便可以过渡到另一个视图控制器。然而,标注不会显示附件视图,我不确定原因。我从firebase中获取一些数据并将其输入到适当的数组中,然后在调用时发布注释。我不知道为什么按钮没有显示出来 此代码发生在ViewDidLoad()中{…} 我也使用了这个函数 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { // If
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// If annotation is not of type RestaurantAnnotation (MKUserLocation types for instance), return nil
var annotationView = self.Map.dequeueReusableAnnotationView(withIdentifier: "Pin")
if annotationView == nil{
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "Pin")
annotationView?.canShowCallout = true
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}else{
annotationView?.annotation = annotation
}
return annotationView
}
编辑:请求使用imgage
你的流量控制很奇怪 为什么只在
如果annotationView==nil
时添加CalloutAccessoryView
请尝试以下代码:
if annotationView == nil{
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "Pin")
}else{
annotationView?.annotation = annotation
}
annotationView?.canShowCallout = true
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
我猜self.Map.dequeueReusableAnnotationView(带标识符:“Pin”)
可能会返回nil,因为我没有看到您的代码
返回值
具有指定标识符的注释视图,如果重用队列中不存在此类对象,则为nil
但是当它无你也需要添加附件我犯了一个非常愚蠢的错误。。。我忘了放置
Map.delegate.self
,因此该函数从未被实际调用是有意义的。按钮现在显示感谢大家的帮助 当我使用此代码时,它没有显示按钮。只有带标题但没有按钮的详图索引可以捕获详图索引附件的图像。。。它是否在Callout附件的右侧留下空白?我添加了一张图片。它没有在那里留下任何空间。
if annotationView == nil{
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "Pin")
}else{
annotationView?.annotation = annotation
}
annotationView?.canShowCallout = true
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)