swift Segue传递旧数据

1tuwyuhd  于 2023-05-16  发布在  Swift
关注(0)|答案(1)|浏览(94)

有人能告诉我为什么我的segue传递旧数据吗?旧的意思是文本“test”被传递,而不是idForPlace的新变量,它首先从API请求中检索它的值。在更改idForPlace的值之后,我是否需要使用完成处理程序或手动准备segue?

...
var idForPlace = "test"
    
    @IBOutlet weak var mapView: GMSMapView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadingInitialMap()
        
    }
 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "Fenster1" {
            let destinationVC = segue.destination as? ViewComments
            destinationVC?.idForPlace = idForPlace
        }
    }

    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
        
        if marker.title == nil {
            print("Kein Marker vorhanden")
        } else {
            print("Vorhandene Marker angeklickt")
            ViewController.titelUebertragen = marker.title!
            let data = marker.userData as? [String:String]
            var idForPlace = (data?["id"])!
                        
            let ref = Database.database().reference()
            ref.child("placeID/\(idForPlace)").observeSingleEvent(of: .value, with: { (snapshot) in
                
            print(idForPlace)
            self.performSegue(withIdentifier: "Fenster1", sender: marker)
            
              })

        }
        
        return true
    }
...
yhuiod9q

yhuiod9q1#

因为您在mapView中有一个要更新的局部变量

var idForPlace = (data?["id"])!

但在prepare(for:sender:)中使用的是为类定义的属性

destinationVC?.idForPlace = idForPlace

所以删除局部变量并执行

self.idForPlace = (data?["id"])!

相关问题