拖动状态结束后,我正在尝试打印出我的帖子模型,但是由于某种原因,它不会打印出任何内容。我已附上我的模型,该模型可以正常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 {
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句