英文:
Segue passes old data
问题
Sure, here is the translated code part:
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("No marker available")
} else {
print("Existing marker tapped")
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
}
Please note that I have translated the code part as per your request, but it's important to keep in mind that some variable and class names may still remain in English since they are often kept in their original form for consistency and readability in code.
英文:
Can someone tell me why my segue passes old data? With old I mean the text "test" gets passed and not the new variable for idForPlace which first retrieves its value from an API request. Do I need to use a completion handler or manually prepare the segue after changing the value of idForPlace?
...
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
}
...
答案1
得分: 1
因为您在mapView
中有一个本地变量,您会更新它:
var idForPlace = (data?["id"])!
但是在您的prepare(for:sender:)
方法中使用的是类定义的属性:
destinationVC?.idForPlace = idForPlace
因此,请移除本地变量,改为:
self.idForPlace = (data?["id"])!
英文:
Because you have a local variable in mapView
that you update
var idForPlace = (data?["id"])!
but what you use in your prepare(for:sender:)
is the property defined for the class
destinationVC?.idForPlace = idForPlace
so remove the local variable and instead do
self.idForPlace = (data?["id"])!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论