MapKit в Swift, часть 2

Я пытаюсь работать с Map Kit в Swift. Пытаюсь отобразить область на карте, один пин (MKPinAnnotationView) и текущую позицию. Все нормально отображаются. Я пытаюсь добавить кнопку раскрытия и перехватить нажатие на нее. Добавлена ​​кнопка раскрытия, но не работает перехват тапа.

Функция pinPressed с методом calloutAccessoryControlTapped не работает....

Это пример кода:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mainMapView: MKMapView!

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

    var objectLatitude = 53.204526
    var objectLongitude = 50.111751

    var currentLatitude = 53.203715
    var currentLongitude =  50.160374

    var latDelta = 0.05
    var longDelta = 0.05

    var currentLocationSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var currentLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude)
    var currentRegion: MKCoordinateRegion = MKCoordinateRegionMake(currentLocation, currentLocationSpan)
    self.mainMapView.setRegion(currentRegion, animated: true)

    var objectLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(objectLatitude, objectLongitude)
    var objectAnnotation = MKPointAnnotation()
    objectAnnotation.coordinate = objectLocation
    objectAnnotation.title = "St. George's Church"
    objectAnnotation.subtitle = "Church of the Great Martyr St. George"
    self.mainMapView.addAnnotation(objectAnnotation)
}

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

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }

        let reuseId = "pin"

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.pinColor = .Purple
            pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as UIButton
        }
        else {
            pinView!.annotation = annotation
        }
        return pinView
}

func pinPressed(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    if control == annotationView.rightCalloutAccessoryView {
        println("Disclosure Pressed!")
    }
}

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

person Alexey Nakhimov    schedule 08.08.2014    source источник


Ответы (2)


Метод делегата calloutAccessoryControlTapped должен называться mapView(annotationView:calloutAccessoryControlTapped:).

Вы не можете использовать свое собственное имя, например pinPressed(...).

Это относится к любому методу делегата и определяется протоколом.

Так должно быть:

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

    if control == annotationView.rightCalloutAccessoryView {
        println("Disclosure Pressed!")
    }
}
person Community    schedule 08.08.2014

Просто обновляю метод делегата calloutAccessoryControlTapped, потому что я попробовал его сегодня (06.09.2015) и не работает. Это поле ввода изменено: annotationView view: MKAnnotationView!

//Click on left or right button
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!)
{
    let anotation = view.annotation as! MyAnnotation

    if (control == view.rightCalloutAccessoryView)
    {
        println("Button right pressed!")
    }
    else if (control == view.leftCalloutAccessoryView)
    {
        println("Button left pressed!")
    }
}
person Thiago Arreguy    schedule 06.09.2015