【问题标题】:How to pass data to another view如何将数据传递到另一个视图
【发布时间】:2016-03-30 09:32:56
【问题描述】:
我正在制作一张有一些标记的地图,我想创建一个信息视图,该视图将显示每个标记的图像和一些文本。
所以,当我按下信息按钮时,它会进入信息视图。
我在代码上有这个:
func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == annotationView.rightCalloutAccessoryView {
performSegueWithIdentifier("showInfo", sender: data)
}
}
但我想从“位置”传递数据,以便为每个位置显示图像。
我需要在 Swift 中执行此操作。
非常感谢。
【问题讨论】:
标签:
ios
swift
mapkit
pass-data
infoview
【解决方案1】:
如果你没有这个方法,你应该将这个方法添加到你的类中,然后将你的数据传递给目标视图的数据。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showInfo" {
let destination = segue.destinationViewController as! YourViewController
//this will execute before next ViewController's viewDidLoad method
destination.data = sender
}
}
【解决方案2】:
您必须在 viewController 中覆盖 prepareForSegue 函数:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if "showInfo" == segue.identifier {
// Then pass your data
let destController = segue.mapViewController as! YourViewController
destController.yourData = sender
}
}
【解决方案3】:
将信息按钮插座连接到您的viewcontroller。然后创建func如下:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if "YourSegue" == segue.identifier {
// Then pass your data
}
}