2016-12-30 5 views
0

私のMKAnnotationsには、クリックすると別のページに分割されるアクセサリ項目があるように、別のチュートリアルを試してみました。しかし、私は表示する項目を得ることができません。ここで私は、さまざまなソース確認した後に作ってみたコードです:MKAnnotationView今すぐAccessoryItemを表示

for place in places { 

     let placeCoord = CLLocationCoordinate2D(latitude: CLLocationDegrees(place.latitude)!, longitude: CLLocationDegrees(place.longitude)!) 

     let annotation = PlaceAnnotation(title: place.title, coordinate: placeCoord, placeObject: place) 
     mapView.addAnnotation(annotation) 

    } 

そしてPlaceAnnotationクラス次のとおりです:

注釈を作成するには、次に

extension MapController : MKMapViewDelegate { 

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

    let identifier = "PlaceAnnotation" 

    if annotation is PlaceAnnotation { 

     var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) 

     if annotationView == nil { 

      annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      annotationView!.canShowCallout = true 

     } else { 

      annotationView!.annotation = annotation 
     } 
     annotationView!.leftCalloutAccessoryView = nil 
     annotationView!.rightCalloutAccessoryView = UIButton(type: UIButtonType.detailDisclosure) 
     return annotationView 
    } 

    return nil 
} 

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
    let place = view.annotation as! PlaceAnnotation 
    let placeName = place.title 
    print(placeName!) 

    let placeInfo = place.placeObject 

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

} 

} 

が、私はこれを持っています

class PlaceAnnotation: NSObject, MKAnnotation { 

var coordinate: CLLocationCoordinate2D 
var title: String? 
var placeObject: Location? 

init(title: String, coordinate: CLLocationCoordinate2D, placeObject: Location) { 
    self.title = title 
    self.coordinate = coordinate 
    self.placeObject = placeObject 
} 
} 

アノテーションにクリックされたときに表示されるのは、タイトルだけですが、他には何も表示されません。私はどんな助けにも感謝します、ありがとう!

(私はスウィフト3で働いています)

+0

あなたのマップビュー作成コードを表示 – Matt

答えて

0

私はあなたのコードを試してみましたが、それは私のために完全に正常に動作します。

しかし、私が言う取り外したときに、デリゲートが呼び出されていないため、

mapView.delegate = self 

アクセサリアイテムは、表示されませんでした。

+0

どのように恥ずかしい...私はmapView.delegateピースハハを書くのを忘れていました。どうもありがとうございます! – JD2017

+0

ようこそ。 – Matt

関連する問題