我试图在拖动状态结束时打印出我的后期模型,但由于某种原因,它不会打印出任何东西。我附上了我的模型,它可以很好地与 viewFor
函数配合使用。提前感谢您的帮助。
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
if newState == MKAnnotationViewDragState.ending {
if let annon = view.annotation?.coordinate as? MyAnno {
print(annon.post?.id) //Not working
view.dragState = MKAnnotationViewDragState.none
}
}
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = MKMarkerAnnotationView()
guard let annotation = annotation as? MyAnno else {return nil}
var identifier = ""
if let dequedView = mapView.dequeueReusableAnnotationView(
withIdentifier: identifier)
as? MKMarkerAnnotationView {
annotationView = dequedView
} else {
annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
}
型号:
class MyAnno: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var post: Post?
var title: String?
init(post: Post, title: String?, subtitle: String?, latitude:CLLocationDegrees, longitude:CLLocationDegrees) {
self.coordinate = CLLocationCoordinate2DMake(latitude, longitude)
self.title = title
}
}
最佳答案
我认为有一个不需要的.coordinate
请更改此行:
if let annon = view.annotation?.coordinate as? MyAnno {
到
if let annon = view.annotation as? MyAnno {
关于swift - 无法从 mapview 中获取自定义模型确实更改了状态函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57720059/