Taylor Simpson
Taylor Simpson

Reputation: 970

How do you add a button to MapKit CallOuts Swift 3

I am trying to add a button to my Callouts so that I can transition to another view controller. The callouts however, won't show the accessory view and I am unsure why. I grab some data from firebase and feed it into the proper arrays, then I have the annotation posted with the call out. I am not sure why the button is not showing up.

This code takes place within ViewDidLoad(){...}

 bookRef.observeSingleEvent(of: .value, with: {snapshot in


            if let userDict = snapshot.value as? [String:AnyObject]{

                for each in userDict as [String:AnyObject]{

                    let bookLats = each.value["bookLat"] as! String

                    let bookLngs = each.value["bookLng"] as! String

                    let bookTitle = each.value["title"] as! String

                    let Author = each.value["Author"] as! String
                    let Comment = each.value["Comment"] as! String
                    let Genre = each.value["Genre"] as! String
                    let User = each.value["User"] as! String
                    let bookPhoto = each.value["bookPhoto"] as! String
                    let userID = each.value["userID"] as! String
                    let userLocation = each.value["userLocation"] as! String
                    let bookRate = each.value["bookRating"] as! String
                    let userToBePushed = each.value["pushingID"] as! String


                    self.bookLatArrayDouble.append((bookLats as NSString).doubleValue)

                    self.bookLngArrayDouble.append((bookLngs as NSString).doubleValue)

                    self.bookTitlesArray.append(bookTitle)



                }



            }



        for i in 0...(self.bookLatArrayDouble.count-1){

           let locationCoordinates = CLLocationCoordinate2D(latitude: self.bookLatArrayDouble[i], longitude: self.bookLngArrayDouble[i])


            var point = BookAnnotation(coordinate: locationCoordinates)

            point.title = self.bookTitlesArray[i]



             self.Map.addAnnotation(point)


        }

I also used this function.

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    // If annotation is not of type RestaurantAnnotation (MKUserLocation types for instance), return nil


    var annotationView = self.Map.dequeueReusableAnnotationView(withIdentifier: "Pin")

    if annotationView == nil{
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "Pin")
        annotationView?.canShowCallout = true
        annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    }else{
        annotationView?.annotation = annotation
    }



    return annotationView
}

Edit: imgage requested

Upvotes: 1

Views: 630

Answers (2)

Taylor Simpson
Taylor Simpson

Reputation: 970

I made a very silly of error... I forgot to put Map.delegate.self so it makes sense that the function was never actually called. The button now shows Thank you all for your help!

Upvotes: 1

Nguyễn Anh Việt
Nguyễn Anh Việt

Reputation: 605

Your flow control is weird

why just when if annotationView == nil then you add CalloutAccessoryView ??

try this code:

if annotationView == nil{
    annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "Pin")  
}else{
    annotationView?.annotation = annotation
}
annotationView?.canShowCallout = true
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

And I'm guess that self.Map.dequeueReusableAnnotationView(withIdentifier: "Pin") may return nil because I don't see your code for that

Return Value

An annotation view with the specified identifier, or nil if no such object exists in the reuse queue.

but when it != nil. you need to add Accessory into it too

Upvotes: 0

Related Questions