首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift MKMapViewDelegate -视图控制器之间的传输坐标

Swift MKMapViewDelegate -视图控制器之间的传输坐标
EN

Stack Overflow用户
提问于 2016-12-23 02:27:41
回答 2查看 472关注 0票数 4

我正在尝试构建一个Map,它可以接收用户输入的纬度和经度坐标,当输入这些输入时,将在另一个选项卡中的地图上放置一个引脚。我的FirstVC由一个按钮“添加位置”组成,用户可以在OtherVC中输入坐标。SecondVC由MapView组成。我最初的想法是有一个特定的坐标数组,任何新的坐标都会附加到这个数组中。执行是我缺少的地方,因为我不知道如何将这个数组传输到MapView。以下是我到目前为止所拥有的:

在输入坐标时:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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
}
}

EN

回答 2

Stack Overflow用户

发布于 2016-12-26 15:34:21

我认为您需要的是从您的ViewController中获取第二个MapViewController,然后传递坐标数组,因此在您的addToMap操作中用下面的

代码语言:javascript
复制
@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
        }
    }

您还需要添加一个导航控制器,如图中所示

我希望这对你有帮助

票数 2
EN

Stack Overflow用户

发布于 2017-01-11 06:43:24

一般来说,我只使用将数据从一个ViewController直接传递到下一个的方法,如果存在父-子关系,并且我可以在prepareForSegue中或在和解除(子到父)中完成。否则,我认为与通知一起使用publisher订阅者模型更好。当您的坐标更改时,您将发布一个通知:

代码语言:javascript
复制
NotificationCenter.default.post(name: NSNotification.Name("MapViewController.coordinate.updated"), object: self, userInfo: nil)

现在,任何关心MapViewController坐标更改的人都可以收听:

代码语言:javascript
复制
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不需要关心谁需要更新;订阅者负责注册自己。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41294425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档