Swift使用NotificationCenter在视图控制器之间传递数据

时间:2017-01-24 06:17:27

标签: swift notifications mkmapviewdelegate

我正在尝试使用通知将用户输入坐标的数据从一个VC传递到另一个VC。当我运行代码时,它没有在MapView上添加注释,所以我有预感我可能没有设置通知与输入的坐标正确发送,但我不知道我哪里出错。

获取坐标输入的类文件:

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)

    }


} 

接收通知并放置注释的类文件:

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

2 个答案:

答案 0 :(得分:1)

它与Objective-C API相同,但使用的是Swift的语法。

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

或者在Swift 3中:

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

如果您的观察者没有从Objective-C对象继承,您必须在方法前加上 @objc ,以便将其用作选择器。

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

}

答案 1 :(得分:0)

注册obeserver以接收MapViewController

中的通知
override func viewDidLoad() {

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

您也可以使用userInfo的{​​{1}}属性发送数据,或使用自定义对象发送。

NSNotification

请参阅pass data using nsnotificationcenter