Big Jeff
Big Jeff

Reputation: 19

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)")
        }
    }
}

Upvotes: 0

Views: 40

Answers (2)

Mohammed Abdullatif
Mohammed Abdullatif

Reputation: 201

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.

Upvotes: 0

Muhammad Shauket
Muhammad Shauket

Reputation: 2778

Check your StatButton if you using in storyboard your buttons , then your button should inherit from StatButton instead of UIButton otherwise your code looks fine.

Upvotes: 1

Related Questions