传递自定义注释的数据

时间:2018-11-24 14:44:16

标签: ios swift mkmapview

如何传递自定义数据?我使用此功能执行segue:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {}

class Custom : MKPointAnnotation { 
    var name:String?
    var time:String?
    var address:String?
}

如果您知道该怎么做,请提供帮助!

1 个答案:

答案 0 :(得分:0)

您可以尝试

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    let anno = view.annotation as! Custom 
    print(anno.name)
    // then access the properties of anno and use them for the segue
    self.performSegue(withIdentifier:"SegueID",sender:anno)
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "SegueID" {

    let des = segue.destination as! SecondVC
    des.ann = sender as! Custom 
  }
}

class SecondVC:UIViewController {

   @IBOutlet weak var lbl:UILabel!
   var ann:Custom!
    override func viewDidLoad() {
       super.viewDidLoad()
       self.lbl.text = ann.name
     }
}