Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在swift中将注释从一个视图传递到另一个视图_Swift - Fatal编程技术网

在swift中将注释从一个视图传递到另一个视图

在swift中将注释从一个视图传递到另一个视图,swift,Swift,我想将注释从ViewController传递到ARViewController,并将其显示在地图上。当我按下segue按钮时,我收到一条Nil错误消息:(以下是我在ViewController中的内容 var startPt = POIs(coordinate: CLLocationCoordinate2D(latitude: Double("")!, longitude: Double("")! )) func mapView(_ mapView: MKMapView, annotation

我想将注释从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
    }
}
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)
}
这就是我在ARViewController中看到的

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
    }
}
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)
}

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

您有两个选择:

  • 在演示之前,只需在
    calloutAccessoryControlTapped
    中设置
    dest
    属性,例如:

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

  • 不要实例化视图控制器,而是以编程方式执行您在这两个视图控制器之间定义的序列(在本例中,我假设您已经为序列本身提供了自己的故事板标识符
    ARV
    ),例如

    如果您这样做,
    prepare(for:sender:)
    将被调用。FWIW,我看不到您在哪里设置
    startPt