didSelect mapView注释无法正常工作

时间:2018-01-25 02:51:46

标签: ios mkmapview swift4 mkannotation mkannotationview

当我点击地图注释时,它将显示有关该引脚的更多信息。

在我第一次点击尝试时,当我点击地图注释时,它将成功运行,它将显示正确的信息,但是当我按下后退按钮并再次尝试使用相同的针时发生了致命错误,并且说它是'在展​​开和可选值时意外发现nil',

基于我的观察,我做了一些故障排除和调试,我发现致命错误只发生在我点击地图引脚之前没有点击地图或者如果我没有点击不同的引脚(如果我第二次尝试点击相同的引脚而不点击地图,则首先发生错误)

import MapKit class MyAnnotations: NSObject,MKAnnotation{ var coordinate: CLLocationCoordinate2D var title: String? var subtitle: String? var img: String? var court_id: Int? var class_name: String? init(coordinate:CLLocationCoordinate2D, title: String?, subtitle: String?, img:String?, court_id: Int?, class_name: String?){ self.coordinate = coordinate self.title = title self.subtitle = subtitle self.img = img self.court_id = court_id self.class_name = class_name super.init() } } 上课:

func mapView(_:didSelect:)

我的func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){ let ibp = view.annotation as? MyAnnotations print(ibp!) ibp_id = (ibp?.court_id)! IdentifierCell = "pin_IBP" performSegue(withIdentifier: "map_ibp_info", sender: self) }

todoDataSet.User.Add[tablename]Row

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
    if view.annotation is MKUserLocation {
        return
    }

    let ibp = view.annotation as? MyAnnotations
    print(ibp!)
    ibp_id = (ibp?.court_id)!
    IdentifierCell = "pin_IBP"
    performSegue(withIdentifier: "map_ibp_info", sender: self)
}