从View Controller MapKit传递数据

时间:2016-11-10 00:43:55

标签: ios swift mapkit segue

我意识到有从视图控制器传递数据的解决方案。但是,我遇到的问题是我使用Map Kit而且我不确定哪个Pin会被点击它可能是以下之一:

 artworkPin = Artwork(title:"Wind Wand",locationName:"Majestic",discipline:"Statue",
                             coordinate:windwandcoord)
 artworkPin2 = Artwork(title:"Wind Wand2",locationName:" Not Majestic",discipline:"Statue",
                         coordinate:windwandcoord2)

我希望ViewTwo(第二个视图控制器)上的标签显示为pin的标题" info"点击的按钮。我目前将其设置为:         var artworkPin:艺术品!

override func viewDidLoad() {
    super.viewDidLoad()
    art_title.text = artworkPin.title

仅加载标签作为artworkPin的标题(第一个引脚)。 必要时附加的代码: ViewTwo ViewControllerOne

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

  

但是,我遇到的问题是我使用Map Kit而且我不确定   哪个Pin会被点击

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == view.rightCalloutAccessoryView {
        if let artworkPin = view.annotation as? Artwork {
            performSegue(withIdentifier: "no", sender: artworkPin)
        }
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let identifier = segue.identifier {
        if identifier == "no" {
            if let artworkPin = sender as? Artwork {
                let ViewTwo = segue.destination as! ViewTwo
                ViewTwo.artworkPin = artworkPin
            }
        }
    }
}