Swift 3 - 在 View Controller 之间传递数据,然后传递给另一个 2

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

我正在尝试执行一个 segue,但它不起作用。 我想要做的是发送我在视图控制器(主)的文本字段中的数据,之后我想将它发送到一个名为 OperationsController 的 ViewController,然后将它发送到另一个视图( CreateController & ListController) 所以我可以使用相同的数据并将其发送到 php 文件并获取数据以填充 ListController 中的 table 视图。并让 CreateController 获取电子邮件(简而言之,数据)并根据电子邮件执行查询并插入数据库。
无论如何,我尝试将数据发送到标签中,但没有用。 这是我的代码

ViewController:

import UIKit

class ViewController: UIViewController {

   var datas:[Usuario]?

    struct Usuario : Codable {
        let correo: String?
        let contrasena: String?

    }

    @IBOutlet weak var txtError: UILabel!
    @IBOutlet weak var txtCorreo: UITextField!

    @IBOutlet weak var txtContra: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func btnLogear(_ sender: Any) {


        let urlString = "http://localhost:8080/swiftdb/logear.php"
        guard let url = URL(string: urlString) else { return }

        URLSession.shared.dataTask(with: url) { (data, response, error) in
            if error != nil {
                print(error!.localizedDescription)
            }

            guard let data = data else { return }
            //Implement JSON decoding and parsing
            do {
                //Decode retrived data with JSONDecoder and assing type of Article object
                let articlesData = try JSONDecoder().decode([Usuario].self, from: data)
                //Get back to the main queue
                DispatchQueue.main.async {
                 self.datas = articlesData

                    let aarti = self.datas

                    for item in aarti! {
                        let correos = item.correo
                        let contras = item.contrasena

                        if(item.correo == self.txtCorreo.text && item.contrasena == self.txtContra.text){

                            let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

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

                           self.performSegue(withIdentifier: "segue", sender: self)


                            self.txtError.text = " "
                        } else {
                            self.txtError.text = "Datos Incorrectos"
                        }
                    }




                }

            } catch let jsonError {
                print(jsonError)
            }


            }.resume()




}

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


}

操作控制器:

import UIKit

class OpcionesController: UIViewController {

    var name: String?

    @IBOutlet weak var displayLbl: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()

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

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

在调用 presentViewController 之前添加:

nextViewController.name = yourTextField.text

您也可以删除 segue 调用。那是多余的。

这是我过去用过的一个例子:

    @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)
    }

1.So 去掉这段代码,因为如果你调用 performSegue 你不需要那个。

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 在 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 这段代码:

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

与:

displayLbl.text = name