快速将注释从一个视图传递到另一个视图

时间:2020-04-03 05:47:57

标签: swift

我想将注释从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)
}

1 个答案:

答案 0 :(得分:0)

您是在手动显示目标视图控制器,而不是执行Segue,因此不会调用prepare(for:sender:)

您有两个选择:

  1. 在出现之前,只需在dest中设置calloutAccessoryControlTapped属性,例如:

    let viewController = storyboard.instantiateViewController(withIdentifier : "ARV") as! ARViewController
    viewController.dest = ...
    

  2. 不要实例化视图控制器,而是以编程方式执行您在这两个视图控制器之间定义的segue(在这种情况下,我假设您已经给出了segue本身, ARV的故事板标识符,例如

    performSegue(withIdentifier: "ARV", sender: self)
    

    如果执行此操作,将调用prepare(for:sender:)。 FWIW,虽然我没有看到您在startPt上的设置位置...