Swift 将数据从注释传递到下一个VC
我有一个带有注释的地图,这些注释有一个细节按钮。此按钮打开滑轮库的抽屉,滑轮库只是覆盖地图的新ViewController。但是我想给这个VC注释标题的信息。到目前为止,我已经做到了:Swift 将数据从注释传递到下一个VC,swift,uiviewcontroller,annotations,Swift,Uiviewcontroller,Annotations,我有一个带有注释的地图,这些注释有一个细节按钮。此按钮打开滑轮库的抽屉,滑轮库只是覆盖地图的新ViewController。但是我想给这个VC注释标题的信息。到目前为止,我已经做到了: func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { if control == view.rightCal
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,因为这里的滑轮库还有其他问题 使用让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)
}
}
请让我知道它是否有效
如果没有,你可以考虑使用委托。
让vcbar=BarDetailVC()
这是一个全新的对象vcbar
,你知道吗?它是如何链接到detailVC
?不,它不工作。它说detailVC没有名为barname的成员。它也不适用于BarDetailVC中的任何其他变量。这就是为什么我试图把vcbar放在第一位的原因。使用代理是什么意思?你能解释一下吗?好的,不,我只是没有仔细阅读你的代码。它起作用了!!非常感谢!:)我很高兴我能帮上忙^^请你在我的答案上打绿色勾:P太酷了