Ios 从MapAnnotation获取对象并使用segue传递

Ios 从MapAnnotation获取对象并使用segue传递,ios,swift,mapkit,mkannotation,mkannotationview,Ios,Swift,Mapkit,Mkannotation,Mkannotationview,我有一个显示酒吧地图的应用程序,还有一个信息栏视图控制器,每个酒吧都有详细信息 我用注释在地图上显示位置,当我在中按下注释时,我想用这些数据转到infoVc 我怎么做 我正在使用prepare and perform with segue,并尝试将注释强制转换为Bar对象,但无效 预备-- 执行-- 在我的infoVC中,我有一个infoBar变量来捕获数据 注意-当我尝试传输硬编码条(我创建的示例条)时,它起作用了,因此问题在于将注释强制转换为通用条对象您可以使用didSelectAnnota

我有一个显示酒吧地图的应用程序,还有一个信息栏视图控制器,每个酒吧都有详细信息

我用注释在地图上显示位置,当我在中按下注释时,我想用这些数据转到infoVc

我怎么做

我正在使用prepare and perform with segue,并尝试将注释强制转换为Bar对象,但无效

预备--

执行--

在我的infoVC中,我有一个infoBar变量来捕获数据


注意-当我尝试传输硬编码条(我创建的示例条)时,它起作用了,因此问题在于将注释强制转换为通用条对象

您可以使用didSelectAnnotationView中的选定注释,然后将该注释存储到实例变量,然后在prepare(for segue:method)中使用注释

var getSelectedAnnotation: Bar?

 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if let getBar = view.annotation as? Bar{
     getSelectedAnnotation = getBar
     print("sucess")
     performSegue(withIdentifier: segueMapToInfo, sender: self)
    }
}

   let segueMapToInfo = "mapToDetail"
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == segueMapToInfo {
            let destVC = segue.destination as! InfoViewController
            destVC.infoBar = getSelectedAnnotation
        }
    }
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    let bar = view.annotation as? Bar
    print("sucess")
    performSegue(withIdentifier: segueMapToInfo, sender: bar)
}
var getSelectedAnnotation: Bar?

 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if let getBar = view.annotation as? Bar{
     getSelectedAnnotation = getBar
     print("sucess")
     performSegue(withIdentifier: segueMapToInfo, sender: self)
    }
}

   let segueMapToInfo = "mapToDetail"
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == segueMapToInfo {
            let destVC = segue.destination as! InfoViewController
            destVC.infoBar = getSelectedAnnotation
        }
    }