Netox
Netox

Reputation: 45

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

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 to send it to a ViewController called OperationsController and after that send it to another Views (CreateController & ListController) so i can use that same data and send it to a php file and get data to populate a table view in ListController. And for CreateController to get the email (which is in short words the data) and perform a query based on the email and insert into the database.
Anyways i tried sending the data to Operations into a label and doesn't work. This is my code

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


}

OperationsController: .

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.
    }
    */

}

Upvotes: 1

Views: 7062

Answers (2)

Latenec
Latenec

Reputation: 418

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

Upvotes: 1

Jake
Jake

Reputation: 2216

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

Upvotes: 4

Related Questions