user5600884
user5600884

Reputation: 119

Passed Segue variable keeps clearing after load

So I have been working on learning swift and I have come across and issue that has had me stuck for awhile. I am passing the data from the first controller to the second controller using the seques.

When I print the variable onLoad it prints out the correct values passed, however after I execute another function (click on a red button) it doesn't print out the passed values but instead blank values.

var Level = 0;
var userId: String = "";
var clanId: String = "";
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    print(userId); //prints 97
    print(clanId); //prins 2
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func redButtonClick(sender: AnyObject) {
     print(userId); //prints ""
    print(clanId); //prins ""
}

As you can see on in the code it seems to be something pretty simple but obviously I am missing how this works as after the onload the variables contain nothing in them.

Thanks everyone for the help!

------------------EDIT----------------------

Changed the values userId: String = "test" Changed the values clanId: String = "test"

It now prints out in onload function

 print(userId)
 print(clanId)

 results:
 97
 2
 test
 test

----------------ADDITION-----------

@IBAction func loginButton(sender: AnyObject) {
  self.performSegueWithIdentifier("dashboard", sender: self);
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if(segue.identifier == "dashboard")
        {
            let dutydash:dashboardController  = segue.destinationViewController as! dashboardController;
            dutydash.userId = self.userId;
           dutydash.clanId = self.clanId;
        }
}

Upvotes: 0

Views: 443

Answers (3)

ClaytonAV
ClaytonAV

Reputation: 95

The only thing that I can think is that you are maybe running two or more instances of the view controller at the same time. Print "self" at viewdidload and inside the button action and check if both are equal.

Upvotes: 1

kye
kye

Reputation: 2246

Below is a working example, ensure your data is being set in prepareForSegue.

class prevClass: UIViewController{

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "nextView" {
            let destinationVC = segue.destinationViewController as! NextView
            destinationVC.userId = "77789";
            destinationVC.clanId = "Awesome Clan"

        }
    }
    @IBAction func next(sender: AnyObject) {
        self.performSegueWithIdentifier("nextView", sender: self)

    }
}

class NextView: UIViewController {

    var userId: String!
    var clanId: String!

    override func viewDidLoad() {
    }

    @IBAction func redButtonClick(sender: AnyObject) {
        print(userId);
        print(clanId);
    }
}

Upvotes: 2

sschale
sschale

Reputation: 5188

Couple suggestions:

  • Change your initial setting of userId and clanId to something else instead of "", like "test" to see if that's a problem
  • In Swift, you don't need ; at the end of each line
  • All variables should be camel case (starting with lowercase), so change Level to level

Upvotes: 1

Related Questions