gpt4 book ai didi

Swift 使用 NotificationCenter 在 View Controller 之间传递数据

转载 作者:行者123 更新时间:2023-11-28 15:55:25 25 4
gpt4 key购买 nike

我正在尝试使用通知将用户输入坐标的数据从一个 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
}
}

最佳答案

它与 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

关于Swift 使用 NotificationCenter 在 View Controller 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41821258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com