class 的布局

Layout for class

我对两个代码块之间的 link 有疑问,这有望改变注释图像。我是 swift 的新手,所以我很难在 mapView 上找到它。我在 运行s 时在函数中给出了打印字符串的代码,但该字符串未显示在 NSLog 中。这是否意味着该函数根本不是 运行?对于我哪里出错的任何建议,我将不胜感激。谢谢

class ViewController2: UIViewController {

@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()

//Location for first Pin
let locationOne = CLLocationCoordinate2DMake(-47.016945, 167.852095)

//Location for map centering
let locationNZ = CLLocationCoordinate2DMake(-43.937462, 170.507813)
let span = MKCoordinateSpanMake(9, 9)
let region = MKCoordinateRegion(center: locationNZ, span: span)
mapView.setRegion(region, animated: true)

//Create annotation one
let annotation = MKPointAnnotation()
annotation.coordinate = locationOne
annotation.subtitle = "Park"
annotation.title = "Rakiura National Park"

//Add annotation to the map
mapView.addAnnotation(annotation)}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()}




func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

print("func")
guard !annotation.isKind(of: MKUserLocation.self) else {
return nil}

let annotationIdentifier = "AnnotationIdentifier"

var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation}
 else {

let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView = av}

if let annotationView = annotationView {
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "mapIcon.png")}

return annotationView}}

听起来你可能遇到了我忘记设置代理所以我的代理从来没有被调用的问题;当您的委托方法未触发时,这应该始终是您所怀疑的。尝试

mapView.delegate = self