简体   繁体   中英

Passing Data from 1 view Controller to another

I've tried to use many other strategies but as of yet, have not succeeded.

I've made a true or false game. If you get the answer right, your score increases by one point. If you get it wrong, you're sent through to a GameOver View Controller.

What I'm trying to do is to have the Score you got, transfer from the SecondVC to the GameOverVC.

var Score = 0

if AnswerNumber == 0 {
    Score += 1
    Score_Keeper.text = NSString(format:"%i",Score) as String

(Score_Keeper is the name of my label which shows you your current Score on SecondVC)

else {

     let destinationController = storyboard?.instantiateViewControllerWithIdentifier("ThirdVC")
     presentViewController(destinationController!, animated: true, completion: nil)

This brings me to my GameOverVC. To assign the "Score" to my UILabel in the GameOver Screen I have written at the very end of my code.

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

    SecondVC.LabelText == Score

LabelText in my GameOverVC is

var LabelText = Int()

And in my View Did Load I've written:

Score_Number.text = "\(LabelText)"

Where Score_Number is the name of my UILabel. Basically, I want my Score_Number in GameOverVC to = Score in the Second VC.

I hope my question is clear... Thank you so much!!

You don't need to implement prepareForSegue. Just assign the value before presenting it

 let destinationController = storyboard?.instantiateViewControllerWithIdentifier("ThirdVC") as GameOverVC
 destinationController.LabelText = score;
 presentViewController(desinationController!, animated: true, completion: nil)

Also in your prepareforSegue assignment should

SecondVC.LabelText = Score

instead of

SecondVC.LabelText == Score

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM