2017-04-13 56 views
0

我目前有一個地圖視圖控制器設置一個引腳到一個位置。當點擊這個引腳時,我希望它能夠轉到另一個視圖控制器,但是它在運行時不會執行任何操作。有任何想法嗎?點擊一個引腳到另一個ViewController

import UIKit 
import MapKit 

class mapquiz: UIViewController { 
    @IBOutlet weak var mapView: MKMapView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let location = CLLocationCoordinate2DMake(33.5206608, -1.8913687) 
     mapView.setRegion(MKCoordinateRegionMakeWithDistance(location, 1500, 1500), animated: true) 

     let pin = PinAnnotation(title: "Birmingham", subtitle:"Second largest city" , coordinate: location) 
     mapView.addAnnotation(pin) 

     func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
      if(segue.identifier == "newView") { 
       var UserViewController = (segue.destination as! UserViewController) 
      } 
     } 
    } 
} 

回答

4

使用委託方法 - didSelectAnnotationView並做進一步處理

1

MKMapViewDelegate有一個名爲mapView(_:didSelect:)方法。你可以在這個方法中執行一個segue。另外,您應該覆蓋prepare(for:sender:),而不是在viewDidLoad()之內。

import UIKit 
import MapKit 

class mapquiz: UIViewController { 

    @IBOutlet weak var mapView: MKMapView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     mapView.delegate = self 

     let location = CLLocationCoordinate2DMake(33.5206608, -1.8913687) 
     mapView.setRegion(MKCoordinateRegionMakeWithDistance(location, 1500, 1500), animated: true) 

     let pin = PinAnnotation(title: "Birmingham", subtitle:"Second largest city" , coordinate: location) 
     mapView.addAnnotation(pin) 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "newView" { 
      let userViewController = segue.destination as! UserViewController 
      // TODO: something 
     } 
    } 
} 

extension mapquiz: MKMapViewDelegate { 

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 
     performSegue(withIdentifier: "newView", sender: nil) 
    } 
} 
+0

sigbart error?任何解決方案 –

+0

你能解釋更多關於這個錯誤嗎?你在使用Swift 2還是3? –

+0

嗨,這是現在工作謝謝,但是當我點擊引腳,它需要我直接到下一個視圖控制器,但我想它,所以當我點擊引腳它顯示標題和副標題,然後當我點擊這是帶我到視圖控制器?謝謝 –

相關問題