Swift - 从地图标注执行转场操作

4

我的地图上有一个标注点。我需要将标注点的标题传递给另一个UIView(在这种情况下是ikinciEkran.swift)。但我无法做到这一点。

以下是我的代码中的segue部分,我不知道如何segue所选标注的标题。

我只粘贴了相关代码的部分。

class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var lbl_Name: UILabel!
@IBOutlet weak var lbl_Address: UILabel!
@IBOutlet weak var lbl_Phone: UILabel!

var LocationManager = CLLocationManager()

var i = 0

var annotation:MKAnnotation!

override func viewDidLoad() {
    super.viewDidLoad()

    LocationManager.delegate = self
    mapView.delegate = self

    LocationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    LocationManager.requestWhenInUseAuthorization()
    LocationManager.startUpdatingLocation()

    let tekel1 = TekelClass(
        title: "tekel1",
        locationName: "Tekelİsim1",
        coordinate: CLLocationCoordinate2D(latitude: 40.9400, longitude: 29.300520))

    mapView.addAnnotation(tekel1)

}

 func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!,
    calloutAccessoryControlTapped control: UIControl!) {
        if control == view.rightCalloutAccessoryView {
            performSegueWithIdentifier("toTheMoon", sender: self)

        }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "toTheMoon" )
    {
        var ikinciEkran = segue.destinationViewController as! DetailViewController

        ikinciEkran.tekelName = "how to segue title of the selected pin"

    }

}
1个回答

3
您需要实现viewForAnnotation方法。
例如。
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

    if annotation is MKUserLocation {
        return nil
    }

    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.canShowCallout = true

        var rightButton: AnyObject! = UIButton.buttonWithType(UIButtonType.DetailDisclosure)
        rightButton.titleForState(UIControlState.Normal)

        pinView!.rightCalloutAccessoryView = rightButton as! UIView
    }
    else {
        pinView?.annotation = annotation
    }

    return pinView
}

如果用户在注释上点击 (i) 按钮,将调用 calloutAccessoryControlTapped

enter image description here

参考:我的MKMapView示例代码https://github.com/koogawa/MKMapViewSample 编辑
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == view.rightCalloutAccessoryView {
        performSegueWithIdentifier("toTheMoon", sender: view)
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "toTheMoon" )
    {
        var ikinciEkran = segue.destinationViewController as! DetailViewController

        ikinciEkran.tekelName = (sender as! MKAnnotationView).annotation!.title

    }

}

亲爱的Kosuke,如何将pin的标题连结(segue)?在你的Github示例中似乎没有传递连结。 - Numan Bayülken

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接