2016-12-18 18 views
0

我提示用戶從另一個視圖控制器輸入緯度和縱向座標。我不知道如何將這些座標轉換爲地圖,其中可以放置一個別針。以下是我的故事板的設置。Swift從輸入座標中添加圖釘位置

enter image description here

我應該使用UserDefaults轉移保存的座標或全局變量?我不確定什麼是最好的方法來解決這個問題。

回答

1

通過設置其屬性之一,您可以將參數從第一個視圖控制器傳遞到第二個視圖控制器。

這裏的一個一步一步的指導:

1 - 在你的FirstViewController,實現UITabBarControllerDelegate協議

class FirstViewController: UIViewController, UITabBarControllerDelegate { 
    @IBOutlet weak var latitudeField: UITextField! 
    @IBOutlet weak var longtitudeField: UITextField! 

    var coordinates = [CLLocationCoordinate2D]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.tabBarController?.delegate = self 
    } 

    @IBAction func addCoordinates(_ sender: Any) { 
     guard let lat = Double(latitudeField.text!), let long = Double(longtitudeField.text!) else { 
      return 
     } 

     self.coordinates.append(CLLocationCoordinate2D(latitude: lat, longitude: long)) 
    } 

    // This method will be called whenever you are switching tab 
    // Note that the viewController can be the same view controller, i.e. FirstViewController 
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) { 
     guard let secondViewController = viewController as? SecondViewController else { 
      return 
     } 
     secondViewController.coordinates = self.coordinates 
    } 
} 

2 - 在你的SecondViewController顯示銷的座標:

class SecondViewController: UIViewController, MKMapViewDelegate { 
    @IBOutlet weak var mapView: MKMapView! 

    var coordinates = [CLLocationCoordinate2D]() { 
     didSet { 
      // Update the pins 
      // Since it doesn't check for which coordinates are new, it you go back to 
      // the first view controller and add more coordinates, the old coordinates 
      // will get a duplicate set of pins 
      for (index, coordinate) in self.coordinates.enumerated() { 
       let annotation = MKPointAnnotation() 
       annotation.coordinate = coordinate 
       annotation.title = "Location \(index)" 

       mapView.addAnnotation(annotation) 
      } 
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     mapView.delegate = self 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
     let identifier = "pinAnnotation" 
     var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView 

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

     annotationView?.annotation = annotation 
     return annotationView 
    } 
} 
+0

啊我看到了,所以你可以通過segue將「secondViewController.coordinate」傳遞給secondViewController嗎?另外,我在我的問題陳述中不清楚,但我不希望按鈕繼續到secondViewController。所以點擊按鈕「添加到地圖!」保留在那個firstViewController中。我如何在不使用segue的情況下傳遞信息? – Kevin

+0

您可以在'FirstViewController'中將座標添加到數組中。當到達地圖視圖的時候,將其設置爲'prepare'方法。 –

+0

我無法正常工作。我的firstViewController和secondViewController在不同的選項卡(選項卡控制器)。我已經實現了對secondViewController代碼的註解委託,並且我試圖在按鈕被擊中後添加引腳,一旦用戶切換選項卡,但是我沒有任何運氣。 – Kevin