How to pass the value of a variable through a button to another class in Swift

0

I have a login screen, with the fields email and password. I need to use in another viewController the email that was used to log in to the system. But I can not pass the typed email in the viewController Login to another viewController.

    
asked by anonymous 16.05.2016 / 01:17

2 answers

2

If you are using Segues , simply overwrite the prepareForSegue method in your ViewController login.

For example, assuming the next ViewController is called NextViewController and the ID of your Follow is GoToNextSegue , the code looks like this: / p>

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "GoToNextSegue" {
        let vc = segue.destinationViewController as! NextViewController
        vc.email = "aqui você informa o e-mail"
    }
}

Obviously, in your NextViewController class you need to have a parameter called email that will receive the information.

    
18.05.2016 / 04:31
1

If you do not use follow, you can proceed as follows

First in VC-B declare the variable that will receive the value.

//VC-B 
class VC_B: UIViewController{
    var Valor:String!

    override func viewDidLoad() {
        super.viewDidLoad()

        print(Valor)
    }
}

//VC_A
class VC_A: UIViewController{

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func AbrirVC_B(sender:UIButton){
       let story = UIStoryboard(name: "Main", bundle: nil)
        let vc:VC_B = story.instantiateViewControllerWithIdentifier("VC_B") as! VC_B
        vc.Valor = "Novo Valor"
        self.presentViewController(vc, animated: false, completion: nil)
    }
}
    
05.07.2016 / 20:18