Как указать, какая кнопка была нажата при вызове метода «calloutAccessoryControlTapped» в MapKit?Swift 4 - PullRequest
0 голосов
/ 14 марта 2019

У меня есть две кнопки на двух сторонах моего annotationView, и я хочу, чтобы они выполняли разные методы в зависимости от того, какая из них нажата.Я использовал calloutAccessoryControlTapped для первой кнопки, но не знаю, как указать метод для второй кнопки (button2).Любая помощь будет принята с благодарностью.

Спасибо

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        // Define a reuse identifier just like with tables or collection views
        let identifier = "Capital"

        if annotation is Capital {
            var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView

            if annotationView == nil {
                // If it isn't able to find a reusable view, create a new one using MKPinAnnotationView and sets its canShowCallout property to true. This triggers the popup with the city name.
                annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                annotationView!.canShowCallout = true

                let button = UIButton(type: .detailDisclosure)
                annotationView!.rightCalloutAccessoryView = button

                let button2 = UIButton(type: .contactAdd)
                annotationView?.leftCalloutAccessoryView = button2

            } else {
                annotationView!.annotation = annotation
            }
             return annotationView
        }
        return nil
    }

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {


        let capital = view.annotation as! Capital
        let placeName = capital.title
        let placeInfo = capital.info

        // let fullImage = UIImage(named: "fullHeart")
        // let emptyImage = UIImage(named: "emptyHeart")

        let ac = UIAlertController(title: placeName, message: placeInfo, preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)


    }

1 Ответ

0 голосов
/ 14 марта 2019

Попробуйте следующий код

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == view.rightCalloutAccessoryView {
        // rightCalloutAccessoryView tapped
    } else if control == view.leftCalloutAccessoryView {
        // leftCalloutAccessoryView tapped
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...