Search code examples
swiftmapkitcallout

MapKit - Displaying image in detailCalloutAccessoryView - accessing mapView.selectedAnnotations not working


I have a subclass of MKPointAnnotation called CustomPointAnnotation:

class CustomPointAnnotation: MKPointAnnotation { var imageName: String! var theItemObject: PFObject? var theImage: UIImage? }

In the view controller dealing with the map (and yes I have the class set to the MKMapViewDelegate), I run a function called getAnnotations() which successfully adds my various annotations to the map (which happen to be objects retrieved from Parse). Each annotation object has an image associated with it, that's the theImage in the class above.

My confusion is how viewForAnnotation gets called before didSelectAnnotationView. Because of this, I am unable to extract the image stored in the class, which I want to use for the detailCalloutAccessoryView of the callout.

Below is my viewForAnnotation function:

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

    if !(annotation is CustomPointAnnotation) {
        return nil
    }

    print("viewForAnnotation was called!")


    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)

    if anView == nil {

        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView!.canShowCallout = true

    }

    else {
        anView!.annotation = annotation
    }


    //Set annotation-specific properties **AFTER**
    //the view is dequeued or created...



    let cpa = annotation as! CustomPointAnnotation

    anView!.image = UIImage(named:cpa.imageName)

    let rightButton: UIButton = UIButton(type: .DetailDisclosure)
    rightButton.setImage(UIImage(named: "arrow-icon.png"), forState: .Normal)
    rightButton.tintColor = UIColor.grayColor()
    rightButton.addTarget(self, action: Selector("showItemDetail:"), forControlEvents: UIControlEvents.TouchUpInside)
    anView!.rightCalloutAccessoryView = rightButton

    print("about to print the array of selected annotations")
    print(mapView.selectedAnnotations)


    if mapView.selectedAnnotations.count > 0 {

        print("THIS IS NEVER SATISFIED, I WILL NEVER GET HERE, WHY?!")

        //yes, there is an annotation currently selected

        if let selectedAnnotation = mapView.selectedAnnotations[0] as? CustomPointAnnotation {

            fullImageOfItem = selectedAnnotation.theImage

            print("i just set the image")

        }
    }

    else {
        print("there are no selected annotations")
    }




    let myCustomImage: UIImageView = UIImageView(image: fullImageOfItem)

    anView!.detailCalloutAccessoryView = myCustomImage

    return anView

}

I don't understand why mapView.selectedAnnotations.count is always 0. Because of this, I am unable to set my fullImageOfItem variable, which is then set to the detailCalloutAccessoryView a little farther down.


Solution

  • Okay, so I think I figured it out:

    I removed the if/else checking mapView.selectedAnnotations.

    let cpa = annotation as! CustomPointAnnotation
    
        anView!.image = UIImage(named:cpa.imageName)
    
        let rightButton: UIButton = UIButton(type: .DetailDisclosure)
        rightButton.setImage(UIImage(named: "arrow-icon.png"), forState: .Normal)
        rightButton.tintColor = UIColor.grayColor()
        rightButton.addTarget(self, action: Selector("showItemDetail:"), forControlEvents: UIControlEvents.TouchUpInside)
        anView!.rightCalloutAccessoryView = rightButton
    
        fullImageOfItem = cpa.theImage
    
        let myCustomImage: UIImageView = UIImageView(image: fullImageOfItem)
    
        //anView!.leftCalloutAccessoryView = myCustomImage
    
        anView!.detailCalloutAccessoryView = myCustomImage
    
        return anView
    

    I simply set my fullImageOfitem variable to cpa.theImage which is an instance of my custom annotation class. In the first line of the code, I set cpa to the annotation (passed into the function automatically) cast as my CustomPointAnnotation. And it seems to be working!