didSelect mapView 注释无法正常工作

didSelect mapView annotation not working properly

当我点击地图注释时,它会显示有关图钉的更多信息。

在我的第一次点击尝试中,当我点击地图注释时它会 运行 成功并且它会显示正确的信息但是当我点击后退按钮并第二次尝试点击相同的图钉时致命的发生错误,它说 'Unexpectedly Found nil while unwrapping and optional value',

根据我的观察并进行了一些故障排除和调试,我发现只有在我点击地图图钉之前没有先点击地图或者我没有点击不同的地图时才会发生致命错误pin(如果我第二次尝试点击相同的 pin 而没有点击地图,则会发生第一次错误)

MyAnnotations Class:

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

试试下面的代码:

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