TextFields don't get updated with values in unwind function Swift

早过忘川 提交于 2019-12-11 15:25:22

问题


I have textfields that should get a value displayed once returning from a TableViewController where user selects a cell. I get that value in unwindfunction, but textfieldsdon't get updated. When printing the value it prints correctly on unwinding, so unwindshould be set correctly, but it just don't get displayed in it's textfield. I also tried prepare(for unwind:in TableViewControllerbut with same results. Can you see what I'm doing wrong? As always many thanks.

unwind function:

@IBAction func unwindToDetailsVc(segue: UIStoryboardSegue) {
        //Insert function to be run upon dismiss of VC2
        print("unwindSegue triggered")
        if let vc = segue.source as? CityTableViewController {
            print("segue source is city vc : \(String(describing: vc.city!))")

            self.cityTextField.text = vc.city
        }
        if let vc = segue.source as? RegionTableViewController {
            print("segue source is region vc : \(String(describing: vc.region!))")
            self.regionTextField.text = vc.region
        }
        if let vc = segue.source as? CountryTableViewController {
            print("segue source is country vc : \(String(describing: vc.country!))")
            self.countryTextField.text = vc.country
        }
    }

didSelect in TableView:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! CityTableViewCell
        self.city = cell.cityLabel.text ?? ""
        performSegue(withIdentifier: "unwindSegue", sender: self)
//        self.dismiss(animated: true, completion: nil)
    }

prepare for unwind:

override func prepare(for unwind: UIStoryboardSegue, sender: Any?) {
        if unwind.identifier == "unwindSegue"  {
            if let detailsVc = unwind.destination as? ShopDetailsTableViewController {
                detailsVc.cityTextField.text! = city
            }
        }
    }

textfield delegate function:

func textFieldDidBeginEditing(_ textField: UITextField) {
        print("Editing textfield")
        if textField.accessibilityIdentifier == "city" {
            print("Editing city textfield")
            performSegue(withIdentifier: "citySegue", sender: self)
        } else if textField.accessibilityIdentifier == "region" {
            print("Editing regio textfield")
            performSegue(withIdentifier: "regionSegue", sender: self)
        } else if textField.accessibilityIdentifier == "country" {
            print("Editing country textfield")
            performSegue(withIdentifier: "countrySegue", sender: self)
        }
    }

回答1:


You can simply use a closure to solve this kind of problem statement,

class ViewController: UIViewController {
    @IBOutlet weak var textField: UITextField!

    @IBAction func openTableVC(_ sender: UIButton) {
        if let controller = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TableViewController") as? TableViewController {
            controller.handler = {[weak self](city) in
                DispatchQueue.main.async {
                    self?.textField.text = city
                }
            }
            self.navigationController?.pushViewController(controller, animated: true)
        }
    }
}

class TableViewController: UITableViewController {
    var handler: ((String)->())?

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let city = "Delhi"
        self.handler?(city)
        self.navigationController?.popViewController(animated: true)
    }
}

The above code is generic and will work in every case from where you want to open TableViewController.




回答2:


I finally found out what I was doing wrong, I was calling the functions loading from CoreDatauser details and displaying them in viewWillAppear. Once I moved them in saveButtonafter the saving to CoreDatafunction call, it all works as expected. Textfield get updated with select values from tableviews.

Many thank to @PGDev for sharing a more convenient way of coding this, without all the if elsestatements and unwinds. Great example of higher level coding.



来源:https://stackoverflow.com/questions/56074793/textfields-dont-get-updated-with-values-in-unwind-function-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!