0

I'm trying to update a label on my first view controller after unwinding from a second. Dfri posted a helpful answer here Passing data with unwind segue but I still have been unable to make my code work.

I understand that viewDidLoad will not be called again after the unwind, but then I don't understand how the view is updated after sending the data back.

In Dfri's answer his first view controller contains this code but I don't know what 'newValue' is. It doesn't look like he has it defined anywhere else unless I'm overlooking something.

 var dataRecieved: String? {
    willSet {
        labelOne.text = newValue
    }
}

@IBOutlet weak var labelOne: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()

    labelOne.text = "Default passed data"
}

 @IBAction func unwindToThisView(sender: UIStoryboardSegue) {
    if let sourceViewController = sender.sourceViewController as? ViewControllerB {
        dataRecieved = sourceViewController.dataPassed
    }
}

I'm assuming he's using the dataRecieved variable with the 'willSet' keyword as a way to update the view once the new data has arrived. But again, I don't know what 'newValue' is or how that is working.

EDIT

Here is my code but I'm getting unexpectedly found nil on 'playerName.text = newValue' when I return from the unwind. The outlet is hooked up fine and works with the initial 'No Player Selected' default value.

class NBARotoHome: UIViewController{

@IBOutlet weak var playerName: UILabel!

var dataRecieved: String? {
    willSet {
        playerName.text = newValue
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    playerName.text = "No Player Selected"

}

@IBAction func prepareForUnwind(segue: UIStoryboardSegue) {

    if let sourceViewController = segue.source as? BuyStats {
        dataRecieved = sourceViewController.selectedPlayer.Name
    }

}

}
Community
  • 1
  • 1
Louis Sankey
  • 481
  • 8
  • 26

1 Answers1

0

try override func viewDidAppear(){

}

Mr. E
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 31 '22 at 19:50