How to pass data from one screen to another after function executed

Asked

Viewed 48 times

1

After running the function of saving data in the database, how can I send the idcep to another screen/Viewcontroller?

I have the following code:

@IBAction func salvarEndereco(_ sender: Any) {

    let parameters = [
        "cep": cepTextField.text!,
        "logradouro": logradouroTextField.text!,
        "bairro": bairroTextField.text!,
        "cidade": cidadeTextField.text!,
        "uf": estadoTextField.text!,
        "opcao": NSNumber(value: 1)] as Dictionary<String, AnyObject>

    //create the url with URL
    let url = URL(string: "http://www.vigilantescomunitarios.com/php/salvaEndereco.php")! //change the url

    //create the session object
    let session = URLSession.shared

    //now create the URLRequest object using the url object
    var request = URLRequest(url: url)
    request.httpMethod = "POST" //set http method as POST

    do {
        request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body
        //print("Dados", request) dados salvos vindo no request

    } catch let error {
        print(error.localizedDescription)
    }

    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    //create dataTask using the session object to send data to the server
    let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in

        guard error == nil else {
            return
        }

        guard let data = data else {
            return
        }
        print("Data: ", data)

        do {
            //create json object from data
            if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
                print(json)
                // handle json...
            }

        } catch let error {
            print(error.localizedDescription)
        }
    })
    task.resume()
}

On what part of that code can I send the data to another? The data I want to send is in the date object

1 answer

0

Gustavo, good morning,

In fact the responsibility for the request should not be of your viewController, however, as you did so, I believe it would be good to you create a follow for the next viewController and in prepareForSegue you pass the object.

Example:

//Faz a chamada da outra viewController
self.performSegue(withIdentifier: "identifier do segue", sender: nil)
//Implementa a função do prepareSegue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let proximaViewController = segue.destination as? ProximaViewController, segue.identifier == "identifier do segue"{
        proximaViewController.objeto = objetoQueVaiPassar
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.