我想将注释从ViewController传递到ARViewController并将其显示在地图上。按下segue按钮时,我收到一条Nil错误消息:(这就是ViewController中的内容
var startPt = POIs(coordinate: CLLocationCoordinate2D(latitude: Double("")!, longitude: Double("")! ))
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
let storyboard = UIStoryboard(name: "Main", bundle:nil)
let viewController = storyboard.instantiateViewController(withIdentifier : "ARV")
self.present(viewController, animated: true)
//location.mapItem().openInMaps(launchOptions: launchOptions)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//guard let ViewController = segue.destination as? ARViewController,
if segue.identifier == "ARV"{
let vc = segue.destination as! ARViewController
vc.dest = startPt
}
}
这就是我在ARViewController中拥有的
var dest = POIs(coordinate: CLLocationCoordinate2D(latitude: Double("")!, longitude: Double("")! ))
override func viewDidLoad() {
super.viewDidLoad()
//show the user's current location
mapView.userTrackingMode = .follow
mapView.delegate = self as? MKMapViewDelegate
mapView.addAnnotation(dest)
}
答案 0 :(得分:0)
您是在手动显示目标视图控制器,而不是执行Segue,因此不会调用prepare(for:sender:)
。
您有两个选择:
在出现之前,只需在dest
中设置calloutAccessoryControlTapped
属性,例如:
let viewController = storyboard.instantiateViewController(withIdentifier : "ARV") as! ARViewController
viewController.dest = ...
或
不要实例化视图控制器,而是以编程方式执行您在这两个视图控制器之间定义的segue(在这种情况下,我假设您已经给出了segue本身, ARV
的故事板标识符,例如
performSegue(withIdentifier: "ARV", sender: self)
如果执行此操作,将调用prepare(for:sender:)
。 FWIW,虽然我没有看到您在startPt
上的设置位置...