2017-01-24 7 views
0

通知を使用して、ある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) 

    } 


} 

クラスファイルを注釈:これは、Objective-CのAPIと同じですが、スウィフトの構文を使用しています

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

http://stackoverflow.com/questions/36910965/how-to-pass-data-using-notificationcentre-in-swift-3-0-and-nsnotificationcenter/36911168#を見ます36911168 – Sahil

+0

MapViewControllerクラスの通知にオブザーバを追加していないようです。 – firstinq

答えて

1

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

それともスウィフト3:

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

あなたの観察者はObjective-Cのオブジェクトから継承されていない場合は、セレクタとして使用するためには @objc であなたの方法の前に付ける必要があります。また、あなたがNSNotificationuserInfoプロパティを使用してデータを送信したり、カスタムオブジェクトを使用して送信することができ、あなたのMapViewController

override func viewDidLoad() { 

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

に通知を受け取るために

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

}

+0

MapViewControllerのviewDidLoadにObserverを追加しました。また、funcの@ob​​jプレフィックスも追加しましたが、それでも座標は渡していません。 – Kevin

0

登録obeserver。

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

pass data using nsnotificationcenter

+0

私はオブザーバを追加しました - NotificationCenter.default.addObserver(self、selector:#セレクタ(self.add)、名前:NSNotification.Name( "MapViewController.coordinate.updated")、オブジェクト:nil) - Swift 3.0のバージョンからviewDidLoad 、座標はまだ通過していません。通知は正しく設定できますが、私のコードは座標を解釈するのに間違っていますか? – Kevin

+0

セレクタは#selector(self.add(_ :))として記述する必要があります。 – Sahil

関連する問題