-2

Hello I have a problem refreshing the UILabel fields after updating the json. The json file is updated in the API successfully but no the UILabel when I click that to redirect. here is my code

@IBAction func saveButton(_ sender: AnyObject) {

        //API Update profile and Bearer token
        let token = HPWSLoginManager.shared().saveSuccessResponse.token
        let url = URL(string: "http://51.38.36.76:40/api/v1/updateProfile")
        var request = URLRequest(url: url!)
        request.httpMethod = "PUT"
        request.addValue("Bearer \(token!)", forHTTPHeaderField: "Authorization")
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")

        var userNumberTmp: String?
        var userFirstName: String?
        var userLastName: String?
        var userNumber: String?

        // set new values user
        userNumberTmp = self.number.text
        userFirstName = self.firstNameTextfield.text
        userLastName = self.lastNameTextfield.text
        userNumber = userNumberTmp?.replacingOccurrences(of: " ", with: "")

        let json: [String: Any] = ["usernumber": userNumber!,
                                   "firstname": userFirstName!,
                                   "lastname": userLastName!
        ]
        let jsonData = try? JSONSerialization.data(withJSONObject: json)
        request.httpBody = jsonData

        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data, error == nil else {
                print(error?.localizedDescription ?? "No data")
                return
            }
            let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
            if let responseJSON = responseJSON as? [String: Any] {
                print(responseJSON)

                DispatchQueue.main.async {
                    let alert = UIAlertController(title: "Success", message: "edit Successful!", preferredStyle: .alert)
                    let action = UIAlertAction(title: "OK", style: .default) { (action) in

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

                        // refresh UILabel ???????????????????

                    }

                    alert.addAction(action)
                    self.present(alert, animated: true, completion: nil)
            }
        }
    }

    task.resume()
}

I redirect the page but I do not know how to refresh the data (in UILabel) when redirecting please help me

GeekThuG
  • 3
  • 7

2 Answers2

-1

Try this . You need to pass data to nextViewController as below

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "MyInformation") {
        let vc = segue.destination as! NextViewController
        vc.dataProroprty = "Your Data" // your json data
    }
}

In Next ViewController

var dataProperty = ""

  override func viewDidLoad() {
        super.viewDidLoad()
        self.lbl.text = dataProperty
    }
KKRocks
  • 8,222
  • 1
  • 18
  • 84
-1
 DispatchQueue.main.async {
                    let alert = UIAlertController(title: "Success", message: "edit Successful!", preferredStyle: .alert)
                    let action = UIAlertAction(title: "OK", style: .default) { (action) in

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

                        // refresh UILabel ???????????????????

lblName.text = //Enter your label value here

                    }
Vinodh
  • 5,262
  • 4
  • 38
  • 68
Nirav Damania
  • 129
  • 1
  • 6