Layout for class, swift


so I have this problem regarding the linking between two blocks of code which will hopefully change the pin annotation on a map to a different image. I am reasonably new to swift and am having a hard time trying to figure out how to run the function mapView. I've added in a message to be printed when the code is run however the message never shows up. Does this mean that the function is not being run at all and if so, I would appreciate any suggestions as to where I went wrong as I'm very new to this. Thank you

class ViewController2: UIViewController {

@IBOutlet var mapView: MKMapView!
override func 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

override func didReceiveMemoryWarning() {

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

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

Show source
| function   | swift   | annotations   | customization   2017-01-06 03:01 1 Answers

Answers ( 1 )

  1. 2017-01-06 06:01

    It sounds like you may have the I forgot to set the delegate so my delegate never got called problem; which should always be what you suspect when your delegate method doesn't fire. Try

    mapView.delegate = self
◀ Go back