5

MKPointAnnotation で MKMapView (デリゲートが正しく設定されている) を使用しています。注釈は、バックグラウンド スレッドで呼び出されるこのメソッドで生成されます。

func updateMapAnnotations() {
    for var i = 0; i < DataManager.getStationList().count; i++ {
        var s = DataManager.getStationList()[i] as Station
        var annotation = MKPointAnnotation()
        annotation.setCoordinate(CLLocationCoordinate2D(latitude: s.latitude, longitude: s.longitude))
        annotation.title = "\(s.id)"
        dispatch_async(dispatch_get_main_queue(), {
            self.mapView.addAnnotation(annotation)
        })
    }
}

ここで、annotationViews が生成されます。

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if (annotation is MKUserLocation) {
        return nil
    }

    let reuseId = "StationAnnotation"

    let annoStation = DataManager.getStationById(annotation.title!.toInt()!)

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {

        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        let base = UIView(frame: CGRect(x: 0, y: 0, width: 67, height: 26))

        let imageView = UIImageView(frame: CGRect(x: 2, y: 2, width: 22, height: 22))
        imageView.image = UIImage(named: "test")

        base.layer.cornerRadius = 3.0
        base.clipsToBounds = true
        base.backgroundColor = UIColor.whiteColor()

        var priceLabelBig = UILabel(frame: CGRect(x: 25, y: 0, width: 30, height: 25))
        priceLabelBig.textColor = UIColor.blackColor()
        priceLabelBig.font = UIFont(name: priceLabelBig.font.fontName, size: 15)

        var priceLabelSmall = UILabel(frame: CGRect(x: 55, y: 0, width: 12, height: 15))
        priceLabelSmall.textColor = UIColor.blackColor()
        priceLabelSmall.font = UIFont(name: priceLabelBig.font.fontName, size: 12)

        if let curPrice = annoStation?.getTextWithSettings().description {
        var stringLength = countElements(curPrice)
        var substringToIndex = stringLength - 1
            priceLabelBig.text = curPrice.substringToIndex(advance(curPrice.startIndex, substringToIndex))
            priceLabelSmall.text = curPrice.substringFromIndex(advance(curPrice.startIndex, substringToIndex))
        }

        base.addSubview(imageView)
        base.addSubview(priceLabelBig)
        base.addSubview(priceLabelSmall)

        anView.addSubview(base)
        anView.canShowCallout = true
    }
    else {
        anView.annotation = annotation
    }

    return anView
}

「didSelectAnnotationView」を機能させるには、注釈のタイトルを設定し、「canShowCallOut」を true に設定する必要があることを知っています。ご覧のとおり、両方とも正しく設定されています。

だから私が持っているのはmapView(デリゲートが設定されている)で、「canShowCallOut」はtrueで、タイトルが設定されて機能しています。

詳細ページに移動するには、annotationViews ('didSelectAnnotationView') のタップを追跡したいのですが、呼び出されません。

私は何を間違っていますか?

4

1 に答える 1