0

I have these JSON data:

{"login":"ET001","email":"email@try.com"}

In Swift 3.0, I created two files which are LoginVC and ViewController.

ViewController can only be accessed after LoginVC verified the credentials. So far I managed to make the login access the ViewController page based on "success" JSON data from database.

But my next goal is to pass the JSON data "[login]" from LoginVC into ViewController.

In ViewController, I created UILabel "loginLbl" to display the JSON value from LoginVC.

How do update my code?

LoginVC.swift

import UIKit
class LoginVC: UIViewController {

    @IBOutlet var _login: UITextField!
    @IBOutlet var _pass: UITextField!
    @IBOutlet var outputLbl: UILabel!

    var login: String!
    var pass: String!

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

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "tocey"{
        if let destination = segue.destination as? ViewController {
            destination.passedData = self.outputLbl.text
            print("Sender value is : \(sender)")
        }
    }
}

@IBAction func loginData(_ sender: Any) {

    login = _login.text
    pass  = _pass.text

    if(login == "" || pass == "") {

        return
    }
    else {
        let url             = URL(string: "http://localhost/login.php")
        let session         = URLSession.shared

        let request         = NSMutableURLRequest(url: url! as URL)
        request.httpMethod  = "POST"

        let paramToLogin    = "login=\(login!)&pass=\(pass!)"
        request.httpBody    = paramToLogin.data(using: String.Encoding.utf8)

        let task = session.dataTask(with: request as URLRequest, completionHandler: {
            (data, response, error) in
            if error != nil {
                return
            }
            else {
                do {
                    if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String] {
                        DispatchQueue.main.async {
                            let success  = Int(json["success"]!)
                            let loginvaluefromdb = json["login"]
                            if(success == 1){
                                    self.outputLbl.text = loginvaluefromdb;

                                    let abc = json["login"]
                                    self.performSegue(withIdentifier: "tocey", sender: abc)
                                    return
                                }
                            }
                        }
                    }
                    catch {
                    }
                }
            })
            task.resume()
        }
    }
}

ViewController.swift

import UIKit
class ViewController: UIViewController {
    @IBOutlet var loginLbl: UILabel!

    var passedData: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        loginLbl.text = passedData
    }
}

How to pass it into UILabel loginLbl?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AlotJai
  • 147
  • 4
  • 18

2 Answers2

0

Once you identify that login data is correct in your response you need to push your viewController in navigation controller and take one dictionary in your viewController and assign json to that dictionary.

if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String] {
                        DispatchQueue.main.async {
                            let success  = Int(json["success"]!)
                            let loginvaluefromdb = json["login"]
                            if(success == 1){
                                let viewController = self.storyboard?.instantiateViewController(withIdentifier: "yourViwController") as! yourViwController
                    viewController.dictJson = json
                    self.navigationController?.pushViewController(viewController, animated: true)
                            }
Pankaj K.
  • 535
  • 8
  • 18
0
 if(success == 1){
           self.outputLbl.text = loginvaluefromdb;
           // Here you trigger the segue
           self.performSegue(withIdentifier: "goToViewController", sender: loginvaluefromdb)
           return
         }

You need to pass the data in prepare for segue method : Here is the editec code

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToViewController" {
        if let destination = segue.destination as? ViewController {
            // Here you will copy tha data you want to pass
            destination.passedData = sender as? String
            print("Sender Value: \(sender)")
        }
    }
}
RavikanthM
  • 468
  • 3
  • 17