我的 FBSingleClusterView
中有一个名为 companyString
的字符串。但是我似乎无法访问,因为 MKAnnotationView
不会更改为我的 FBSingleClusterView
?
我的代码
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
pinView = FBSingleClusterView(annotation: annotation, reuseIdentifier: reuseId) as FBSingleClusterView
pinView.companyString = singleAnnotation.name
}
但是我一直收到以下错误 MKAnnotationView 类型的值没有成员 companyString
。
为什么不将其转换为 FBSingleClusterView
,它是 MKAnnotationView 的子类?
FBSingleClusterView
class FBSingleClusterView: MKAnnotationView {
var bubbleView: BubbleView?
var addressString: String?
var companyString: String?
var logoImage: UIImage?
override init(annotation: MKAnnotation?, reuseIdentifier: String?){
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
// change the size of the cluster image based on number of stories
backgroundColor = UIColor.clearColor()
setNeedsLayout()
}
required override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
// Images are faster than using drawRect:
centerOffset = CGPointMake(0, -image!.size.height/2);
}
}
最佳答案
你的第一行是
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
因此,pinView 现在是类型 MKAnnotationView?
Why come it isn't being casted into a FBSingleClusterView, which is a subclass of the MKAnnotationView?
一旦设置了 var 类型,就不能重新分配其他类型。
建议的解决方案是
if let pinView:FBSingleClusterView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? FBSingleClusterView
{
pinView.companyString = singleAnnotation.name
}
关于ios - 无法在 MKAnnotationView 上转换子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33341591/