Search code examples
iosswiftsegue

Prepare for segue function not loading new values


The statValue attribute of my UIButton class has been updated since the segue was last called, but the segue still sends the old, original value. Is there a way to refresh the prepare function (below) so that it sends the new value?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "StatDetailSegue" {
        if let destinationVC = segue.destination as? StatDetailViewController,
            let index = (sender as? StatButton)?.buttonIndex,
            let sendVal = (sender as? StatButton)?.buttonValue,
            let sendUnit = (sender as? StatButton)?.buttonUnit,
            let sendTitle = (sender as? StatButton)?.detailTitle {
            destinationVC.statID = index
            destinationVC.statValue = sendVal
            destinationVC.statUnit = sendUnit
            destinationVC.statTitle = sendTitle
            print("Destination STATID: \(destinationVC.statID)")
            print("Destination value: \(destinationVC.statValue)")
        }
    }
}

Solution

  • Can you debug the value of statValue in your destinationVC and check if it gets updated after the destinationVC is presented? Also, check the implementation of 'StatButton' class, maybe the buttonValue property is a lazily initialized property and initialized only once? That's why maybe you keep getting the first value that was assigned to the buttonValue always.