我正在尝试构建一个Map,它可以接收用户输入的纬度和经度坐标,当输入这些输入时,将在另一个选项卡中的地图上放置一个引脚。我的FirstVC由一个按钮“添加位置”组成,用户可以在OtherVC中输入坐标。SecondVC由MapView组成。我最初的想法是有一个特定的坐标数组,任何新的坐标都会附加到这个数组中。执行是我缺少的地方,因为我不知道如何将这个数组传输到MapView。以下是我到目前为止所拥有的:
在输入坐标时:
import UIKit
import CoreLocation
class OtherVC: UIViewController {
@IBOutlet weak var latitudeField: UITextField!
@IBOutlet weak var longitudeField: UITextField!
var coordinates = [CLLocationCoordinate2D]()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func addToMap(_ sender: Any) {
let lat = Double(latitudeField.text!)
let long = Double(longitudeField.text!)
self.coordinates.append(CLLocationCoordinate2D(latitude: lat!, longitude: long!))
}
}对于MapView:
import UIKit
import MapKit
class MapViewController: 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
}
}

发布于 2016-12-26 15:34:21
我认为您需要的是从您的ViewController中获取第二个MapViewController,然后传递坐标数组,因此在您的addToMap操作中用下面的
@IBAction func addToMap(_ sender: Any) {
let lat = Double(latitudeField.text!)
let long = Double(longitudeField.text!)
self.coordinates.append(CLLocationCoordinate2D(latitude: lat!, longitude: long!))
//here we pass the coordinate array to mapViewController
if let mapViewController = self.tabBarController?.viewControllers?[1] as? MapViewController
{
mapViewController.coordinates = self.coordinates
}
}您还需要添加一个导航控制器,如图中所示

我希望这对你有帮助
发布于 2017-01-11 06:43:24
一般来说,我只使用将数据从一个ViewController直接传递到下一个的方法,如果存在父-子关系,并且我可以在prepareForSegue中或在和解除(子到父)中完成。否则,我认为与通知一起使用publisher订阅者模型更好。当您的坐标更改时,您将发布一个通知:
NotificationCenter.default.post(name: NSNotification.Name("MapViewController.coordinate.updated"), object: self, userInfo: nil)现在,任何关心MapViewController坐标更改的人都可以收听:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(coordinateUpdated), name: NSNotification.Name("coordinate.updated"), object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
@objc private func coordinateUpdated( notification: Notification) {
if let source = notification.object as? MapViewController {
print(source.coordinates)
}
}这使得视图松散耦合,MapViewController不需要关心谁需要更新;订阅者负责注册自己。
https://stackoverflow.com/questions/41294425
复制相似问题