Roberto Viramontes Roberto Viramontes - 6 months ago 37
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


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.