2017-03-06 4 views
2

나는 클래스의 배열을 가지고 있습니다. mkmapview에서 몇 가지 주석 핀을 추가합니다. 지도의 대리인으로주석 핀 탭 이벤트 가져 오기 MapKit Swift

var events = [Events]() 

    for event in events { 
     let eventpins = MKPointAnnotation() 
     eventpins.title = event.eventName 
     eventpins.coordinate = CLLocationCoordinate2D(latitude: event.eventLat, longitude: event.eventLon) 
     mapView.addAnnotation(eventpins) 
    } 

내가 도청되고있는 배열 events의 행

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 
    print(view.annotation?.title! ?? "") 
} 

는 어떻게 얻을 수있는 기능을 구현했습니다? 다른 ViewController에서 segue하고 싶고이 Class 객체를 보내려고합니다. 당신이 당신의 주석을 추가 할 때 사용자 지정 주석으로 Event를 연결합니다, 그리고

class EventAnnotation : MKPointAnnotation { 
    var myEvent:Event? 
} 

: : 지금

for event in events { 
    let eventpins = EventAnnotation() 
    eventpins.myEvent = event // Here we link the event with the annotation 
    eventpins.title = event.eventName 
    eventpins.coordinate = CLLocationCoordinate2D(latitude: event.eventLat, longitude: event.eventLon) 
    mapView.addAnnotation(eventpins) 
} 

답변

3

당신은 같은 사용자 정의 주석 클래스를 만들어야합니다 위임 함수에서 이벤트에 액세스 할 수 있습니다.

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 
    // first ensure that it really is an EventAnnotation: 
    if let eventAnnotation = view.annotation as? EventAnnotation { 
     let theEvent = eventAnnotation.myEvent 
     // now do somthing with your event 
    } 
} 
+0

정확히 찾고있는 내용입니다. –