Segue传递旧数据

huangapple go评论73阅读模式
英文:

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"])!

huangapple
  • 本文由 发表于 2023年5月14日 01:06:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定