Ich implementierte Kartenansicht und erstellte einfaches Datenmodell für Anmerkungen.swift, mapView, Anmerkungsansicht wird nicht angezeigt
Die Pins werden auf der Karte angezeigt, aber ich bin immer noch nicht in der Lage, die Details durch Tippen auf einen Pin zu bekommen.
Mein Code:
import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var topView: MapDataView!
var dummyModel: DummyModel?
override func viewDidLoad() {
super.viewDidLoad()
dummyModel = DummyModel()
mapView.delegate = self
mapView.showsUserLocation = true
let region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 1000, 1000)
mapView.setRegion(region, animated: true)
let range = 0..<Int((dummyModel?.objectsArray.count)!)
Array gültig ist und keine nil:
for i in range {
mapView.addAnnotation((dummyModel?.objectsArray[i])!)
}
Die Art und Weise i Anmerkung zu einer Karte hinzufügen:
mapView.selectAnnotation(mapView.annotations[0], animated: true)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
print("viewForAnnotation")
let identifier = "PubMapObject"
if annotation is PubMapObject {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
let btn = UIButton(type: .detailDisclosure)
annotationView!.rightCalloutAccessoryView = btn
} else {
annotationView!.annotation = annotation
}
return annotationView
}
return nil
}
Diese Methode nie angerufen und ich weiß nicht warum. Ich denke, das Problem hier ist, aber ich bin nicht in der Lage, es zu finden):
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
print("annotationView")
let pub = view.annotation as! PubMapObject
}
}
Update Ihr vollständiger Code, da ich denke, didSelect-Methode und andere Methode fehlt in Ihrem Code –