本文介绍了TextFields不会使用展开函数Swift中的值更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个textfields,一旦从TableViewController返回(用户选择一个单元格),它应该会显示一个值.我在unwind函数中获得了该值,但textfields却没有得到更新.打印值时,它在展开时可以正确打印,因此应该正确设置unwind,但是它不会显示在textfield中.我也在TableViewController中尝试了prepare(for unwind:,但结果相同.你能看到我做错了吗?一如既往的感谢.

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.

解开功能:

@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:

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

准备放松:

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

文本字段委托函数:

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

推荐答案

您可以简单地使用 closure 解决此类问题陈述,

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

上面的代码是通用的,可以在您要打开TableViewController的任何位置使用.

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

这篇关于TextFields不会使用展开函数Swift中的值更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 21:13