如何将数据传递给另一个视图

How to pass data to another view

我正在制作一张有一些标记的地图,我想创建一个信息视图,它会显示每个标记的图像和一些文本。

因此,当我按下信息按钮时,它会转到信息视图。

我在代码上有这个:

func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == annotationView.rightCalloutAccessoryView {

        performSegueWithIdentifier("showInfo", sender: data)

    }
}

但我想传递来自 "locations" 的数据,这样我就可以为每个数据显示一张图片。

我需要在 Swift.

中执行此操作

非常感谢。

您应该将此方法添加到您的 class 如果您没有,然后将您的数据传递到目标视图的数据。

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
    }
}

您必须覆盖 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
        }
    }

将信息按钮插座连接到您的 viewcontroller。然后创建func如下:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if "YourSegue" == segue.identifier {
        // Then pass your data
    }
}