Search code examples
swiftseguetextfield

Sending a text entry through segue


I am sending a textfield entry through a segue to appear in a textfield on another ViewController. The issue I'm having is that the segue nor the sending the of textfield entry is working properly. I know that the segue identifier is correct as if I use self.performSegueWithIdentifier("WelcomeSignUp", sender: nil ) it works fine. Can anybody point out an error I have made or a better method to use?

Here is the code from the first ViewController

@IBOutlet var Email: UITextField!

@IBAction func Signup(sender: AnyObject) {

    let email = self.Email.text

    if email == "" {

        displayAlert("Error", message: "Please Enter Your Email Address")

    }   else {

        func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

            if segue.identifier == "WelcomeSignUp" {

                let destination = segue.destinationViewController as! SignUpViewController

                destination.mail = Email.text!

            }

        }
        }
}

Code of receiving ViewController

@IBOutlet var EmailSignUp: UITextField!

var mail = ""

override func viewDidLoad() {
    super.viewDidLoad()

    EmailSignUp.text = mail

}

UPDATED CODE FROM FIRST ViewController

@IBAction func Signup(sender: AnyObject) {

    let email = self.Email.text

    if email == "" {

        displayAlert("Error", message: "Please Enter Your Email Address")

    }   

}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "WelcomeSignUp" {

        let destination = segue.destinationViewController as! SignUpViewController

        destination.mail = Email.text!

    }

}

Solution

  • prepareForSegue(_:sender:) must be an instance method, but you have declared it as a nested (and unused) function inside Signup(_:).

    You need to move it out so it is declared in the same scope as Signup(_:).