Swift 3 - Passing data between a View Controller and after that to another 2

后端 未结 2 1308
-上瘾入骨i
-上瘾入骨i 2021-01-16 03:45

I\'m trying to perform a segue which it doesn\'t work. What i\'m trying to do is send the data which i have in a textfield in my View Controller(Main), after that i want t

相关标签:
2条回答
  • 2021-01-16 04:07

    Before calling presentViewController add :

    nextViewController.name = yourTextField.text

    You could also delete the segue call. That is redundant.

    Here is an example that I've used in the past :

        @IBAction func doSegue(_ sender: UIButton) {
            buttonTag = sender.tag
    
            let storyboard = UIStoryboard (name: "Main", bundle: nil)
            let resultVC = storyboard.instantiateViewController(withIdentifier: "ResultViewController")as! ResultViewController
    
            // Communicate with new VC - These values are stored in the destination
            // you can set any value stored in the destination VC here
            resultVC.firstValue = buttonTag
            resultVC.secondValue = randomOpponentValue()
            self.navigationController?.pushViewController(resultVC, animated: true)
        }
    
    0 讨论(0)
  • 2021-01-16 04:11

    1.So get rid of this code, because if you are calling performSegue you don’t need that one.

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "OP") as! OpcionesController
    self.present(nextViewController, animated:true, completion:nil)
    

    2.Then in the prepareForSegue

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == “YourSegueIdentifier" {
        let destination: OpcionesController = segue.destination as! OpcionesController
        destination.name = txtCorreo.text
      }
    }
    

    3.Replace this code:

    if let nametoDisplay = name {
        displayLbl.text = name
    }
    

    with:

    displayLbl.text = name
    
    0 讨论(0)
提交回复
热议问题