将数据从注释传递到下一个VC

时间:2017-08-22 16:02:18

标签: swift uiviewcontroller annotations

我有一个带注释的地图,这些注释有一个详细信息按钮。此按钮打开滑轮库中的抽屉,这是一个覆盖地图的新ViewController。但我想给这个VC提供注释标题的信息。到目前为止我得到了这个:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == view.rightCalloutAccessoryView{
        let annotation = self.map.selectedAnnotations[0] as MKAnnotation!
        print(((annotation?.title)!)!)

        //These lines belong to the drawer from the pulley library
        (parent as? PulleyViewController)?.setDrawerPosition(position: PulleyPosition(rawValue: 2)!)
        let detailVC:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "BarDetailVC") as UIViewController

        (parent as? PulleyViewController)?.setDrawerContentViewController(controller: detailVC, animated: true)
        //Here I want to give the upcoming VC the title of the annotation
        let vcbar = BarDetailVC()
        vcbar.barname = ((annotation?.title)!)!
    }
}

打印注释会给出正确的标题。但是当我在BarDetailVC中打印变量barname时,它是空的。我认为这种方式似乎不起作用。由于滑轮库的其他问题,我无法在这里使用segues。

1 个答案:

答案 0 :(得分:1)

使用let vcbar = BarDetailVC(),您将创建一个未使用的全新ViewController。

怎么样:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == view.rightCalloutAccessoryView{
        let annotation = self.map.selectedAnnotations[0] as MKAnnotation!
        print(((annotation?.title)!)!)

        (parent as? PulleyViewController)?.setDrawerPosition(position: PulleyPosition(rawValue: 2)!)
        let detailVC: BarDetailVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "BarDetailVC") as BarDetailVC
        detailVC.barname = ((annotation?.title)!)!
        (parent as? PulleyViewController)?.setDrawerContentViewController(controller: detailVC, animated: true)
    }
}

如果有效,请告诉我。

如果它没有考虑使用代表。