user1822824
user1822824

Reputation: 2488

iOS Segue not working as expected when passing value

I have a button with the following code:

@IBAction func buttonPress(sender: AnyObject) {
    performSegueWithIdentifier("newAccount", sender: sender)
}

When the button is pressed it performs a segue. I want to pass a value to the new view controller so I added the following code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "newAccount" {
        if let dvc = segue.destinationViewController as? NewAccountViewController {
            dvc.testValue = "This is my test value I want to pass"
        }
    }
}

The NewAccountViewController doesn't receive the value (I don't get any error).

The code I have in my NewAccountViewController is:

var testValue:String?

When I print(testValue) I don't get anything.

Why isn't this working as expected?

Upvotes: 1

Views: 126

Answers (2)

James Rao
James Rao

Reputation: 170

There are maybe two reasons: 1. identifier is not set as "newAccount" in the storyboard; 2. you have put a navigator view controller into the NewAccountViewController, which would ask you to transfer the destination as UINavigationController instead of NewAccountViewController. Hope this helps.

Upvotes: 0

Leo
Leo

Reputation: 24714

You need to check if the class is set correctly

Like the ViewController in below image

enter image description here

Upvotes: 3

Related Questions