我的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

}

但是,我不断收到以下错误value of type MKAnnotationView has no member companyString

为什么不将其转换为MKAnnotationView的子类FBSingleClusterView呢?

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/

10-12 21:35