2016-12-15 109 views
0

我實現了地圖視圖併爲註釋創建了簡單的數據模型。swift,mapView,註解視圖不會在點擊時顯示

引腳顯示在地圖上,但我仍無法通過點擊任何引腳來獲取詳細信息。

pins screenshot

我的代碼:

import UIKit 
import MapKit 

class MapViewController: UIViewController, MKMapViewDelegate { 

    @IBOutlet weak var mapView: MKMapView! 
    @IBOutlet weak var topView: MapDataView! 

    var dummyModel: DummyModel? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     dummyModel = DummyModel() 

     mapView.delegate = self 
     mapView.showsUserLocation = true 
     let region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 1000, 1000) 
     mapView.setRegion(region, animated: true) 

     let range = 0..<Int((dummyModel?.objectsArray.count)!) 

陣列是有效的,並沒有零:

 for i in range { 
      mapView.addAnnotation((dummyModel?.objectsArray[i])!) 
     } 

我的方式添加註釋到地圖:

 mapView.selectAnnotation(mapView.annotations[0], animated: true) 
    } 

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
     print("viewForAnnotation") 
     let identifier = "PubMapObject" 

     if annotation is PubMapObject { 
      var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) 

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

       let btn = UIButton(type: .detailDisclosure) 
       annotationView!.rightCalloutAccessoryView = btn 
      } else { 
       annotationView!.annotation = annotation 
      } 

      return annotationView 
     } 

     return nil 
    } 

這種方法從來沒有打電話,我不知道爲什麼。我想這個問題是在這裏,但我無法找到它):

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
     print("annotationView") 
     let pub = view.annotation as! PubMapObject 
    }   
} 
+0

更新給你的框架完整的代碼,因爲我認爲didSelect方法和其他方法缺少在您的代碼 –

回答

1

嘗試添加註釋視圖的一套框架返回註釋視圖 //我的註釋課前和返回的註解視圖之前提供的寬度和高度來標註視圖和

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

if (annotation.isKind(of: MKUserLocation.self)) { 
      return nil 
     } 


let reuseId = "PubMapObject" 
     if (annotation.isKind(of: PubMapObject.self)) { 
      let anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
      anView.isEnabled = true 
      anView.isUserInteractionEnabled = true 
      let btn = UIButton(type: .detailDisclosure) 
      btn.frame = CGRect(x: 0, y: 0, width: 50, height: 70) 
      anView!.rightCalloutAccessoryView = btn 
      anView.frame = CGRect(x: 0, y: 0, width: 50, height: 70) 
      return anView 
     } 

    return nil 
} 
0

嘗試使用didSelect函數代替calloutAccessoryControlTapped。

func mapView(_ mapView: MKMapView, didSelect annotationView: MKAnnotationView) { ... 
+0

我試過這種方式,但該方法從未被稱爲 –

相關問題