2016-11-08 13 views
0

私は非常に新しいです(pythonから来ています)。私はMKAnnotationViewの作成に苦労しています。私はray wenderlichのチュートリアルに従いましたが、コードは古くなっているようです。関数呼び出しは機能していないか、ピンの注釈にあることを意図した "i"を生成しません。ここで私は現在、使用していますコードです:MKAnnotationView Swift「情報」ボタンを追加する

import MapKit 

extension ViewController: MKMapViewDelegate { 

// 1 
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
    if let annotation = annotation as? Artwork { 
     let identifier = "pin" 
     var view: MKPinAnnotationView 
     if let dequeuedView = MapView.dequeueReusableAnnotationView(withIdentifier: identifier) 
      as? MKPinAnnotationView { // 2 
      dequeuedView.annotation = annotation 
      view = dequeuedView 
     } else { 
      // 3 
      view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      view.canShowCallout = true 
      view.calloutOffset = CGPoint(x: -5, y: 5) 
      let button = UIButton(type:.detailDisclosure) 
      view.rightCalloutAccessoryView = button as UIView 
     } 
     return view 
    } 
    return nil 
    } 
} 

私はXcodeの8.1を実行している、任意のヘルプは大歓迎されます。

答えて

2

Xcode 8.1とSwift 3.0を使用しています。あなたはストーリーボードにデリゲートを設定するまで、デリゲートメソッドが発射されていないいくつかの理由のために:ボタンアクション

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
     if control == view.rightCalloutAccessoryView { 
      print("button tapped") 
     } 
    } 

ため

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? 
    { 
     if annotation is MKUserLocation {return nil} 

     let reuseId = "pin" 

     var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView 
     if pinView == nil { 
      pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
      pinView!.canShowCallout = true 
      pinView!.animatesDrop = true 
      let calloutButton = UIButton(type: .DetailDisclosure) 
      pinView!.rightCalloutAccessoryView = calloutButton 
      pinView!.sizeToFit() 
     } 
     else { 
      pinView!.annotation = annotation 
     } 


     return pinView 
    } 

私は「あなたの問題のためのサンプルプロジェクト

Map Sample Project Swift 3. 0 Xcode 8.1

+0

を添付しています次のエラーが表示されます.ViewControllerでMapView.delegate = selfを使用していないため、値が 'View Controller'からt ype 'MKMapView Delegate?' _ –

+0

これはサンプルです。 Swift 3コード。あなたの要望のスウィフト下位バージョンに変更してください – Vinodh

+0

私はすぐに実行していると信じています3 –

関連する問題