Swift MKMapViewDelegate - Passing coordinates between view controllers

I am trying to create a map application that can accept user inputs of latitude and longitude coordinates, which when input puts the output on the map on another tab. My FirstVC consists of a button "Add locations", which goes into "OtherVC", which the user can enter in the coordinates. SecondVC consists of MapView. My initial idea is to have a specific array of coordinates, and any new coordinates will be added to this array. Execution is what I am missing because I am not sure how to pass this array to MapView. Here is what I still have:

To enter coordinates:

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!))
}




}

For 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
}
}

enter image description here

+4
2

, ViewController MapViewController tabBarController, , 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
        }
    }

, enter image description here

, .

+2

, ViewController , , prepareForSegue in unwind ( ). , . :

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 , ; .

+1

Source: https://habr.com/ru/post/1664630/


All Articles