0

I am trying to pass the data of user-input coordinates from one VC to another using Notifications. When I run the code, it does not add an annotation onto the MapView, so I have a hunch I may have not set up the notification to send with the inputted coordinates properly, but I am not sure where I have went wrong.

Class file that takes the input of coordinates:

import UIKit
import CoreLocation

var locations: [Dictionary<String, Any>] = [] // here I initialize my empty array of locations

class OtherVC: UIViewController {

    @IBOutlet weak var latitudeField: UITextField!
    @IBOutlet weak var longitudeField: UITextField!

    @IBOutlet weak var titleTextField: UITextField!
    var coordinates = [CLLocationCoordinate2D]()

    override func viewDidLoad() {
        super.viewDidLoad()
    }




    @IBAction func addToMap(_ sender: Any) {
        let lat = latitudeField.text!
        let long = longitudeField.text!
        let title = titleTextField.text!
        var location: [String: Any] = ["title": title, "latitude": lat, "longitude": long]
        locations.append(location) 
        NotificationCenter.default.post(name: NSNotification.Name("MapViewController.coordinate.updated"), object: locations, userInfo: nil)

    }


} 

Class file that receives notification and places annotation:

import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {


    @IBOutlet weak var mapView: MKMapView!



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

    func add(notification: NSNotification) {

        let dict = notification.object as! NSDictionary

        // takes the coordinates from the notification and converts such that the map can interpret them
        let momentaryLat = (dict["latitude"] as! NSString).doubleValue
        let momentaryLong = (dict["longitude"] as! NSString).doubleValue
        let annotation = MKPointAnnotation()
        annotation.title = dict["title"] as? String
        annotation.coordinate = CLLocationCoordinate2D(latitude: momentaryLat as CLLocationDegrees, longitude: momentaryLong as CLLocationDegrees)
        mapView.addAnnotation(annotation)
        self.mapView.centerCoordinate = annotation.coordinate
    }


    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
    }
} 
Kevin
  • 1,189
  • 2
  • 17
  • 44
  • http://stackoverflow.com/questions/36910965/how-to-pass-data-using-notificationcentre-in-swift-3-0-and-nsnotificationcenter/36911168#36911168 – Sahil Jan 24 '17 at 06:26
  • It looks like you didn't add any observer for the notification in your MapViewController class. – firstinq Jan 24 '17 at 06:33

2 Answers2

1

It's the same as the Objective-C API, but uses Swift's syntax.

NSNotificationCenter.defaultCenter().addObserver(
self,
selector: #selector(batteryLevelChanged),
name: UIDeviceBatteryLevelDidChangeNotification,
object: nil)

Or in Swift 3:

NotificationCenter.default.addObserver(
self,
selector: #selector(self.batteryLevelChanged),
name: .UIDeviceBatteryLevelDidChange,
object: nil)

If your observer does not inherit from an Objective-C object, you must prefix your method with @objc in order to use it as a selector.

@objc func batteryLevelChanged(notification: NSNotification){     
//do stuff

}

amit soni
  • 2,183
  • 1
  • 14
  • 17
  • I have added the Observer in the viewDidLoad of MapViewController, as well as @obj prefix for my func, but it still is not passing the coordinates. – Kevin Jan 24 '17 at 21:12
0

Register obeserver to receive notification in your MapViewController

override func viewDidLoad() {

  NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.add(_:)), name: NSNotification.Name("MapViewController.coordinate.updated"), object: nil)
}

Also you can send data using userInfo property of NSNotification or send using custom object.

func add(_ notification: NSNotification) {
 ...
}

see pass data using nsnotificationcenter

Community
  • 1
  • 1
Sahil
  • 9,096
  • 3
  • 25
  • 29
  • I have added the observer - NotificationCenter.default.addObserver(self, selector: #selector(self.add), name: NSNotification.Name("MapViewController.coordinate.updated"), object: nil) - Swift 3.0 Version to viewDidLoad, and the coordinate still has not passed. Could the notifications be set up properly, but my code not be correct for interpreting the coordinates? – Kevin Jan 24 '17 at 21:19
  • selctor should be written as #selector(self.add(_:)). – Sahil Jan 25 '17 at 05:36