Roberto Viramontes Roberto Viramontes - 1 year ago 91
Swift Question

No displaying the score on the last ScoreViewController Swift

Im having trouble with my segue function, Im trying to pass the final score to the second view but is not displaying on the label this is what i have for my segue function:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationScore : ScoreViewController = segue.destinationViewController as! ScoreViewController

destinationScore.scoreBoard?.text = String(finalScore)

Here is the second view controller.

@IBOutlet weak var scoreBoard: UILabel!

override func viewDidLoad() {


PS: My NSLog is displaying the final score on the terminal but not on the label. Thank you

Answer Source

When prepareForSegue performed, the contents (including view and subviews) of ScoreViewController haven't been loaded yet. So the scoreBoard label was nil when you set the value for label. Therefore the scoreBoard label can't show your score.

To resolve this problem:

  • You'd better add property var score: String in ScoreViewController, then assign text for label in viewDidLoad function.

  • In prepareForSegue, assign score property instead of scoreBoard label.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download