gpt4 book ai didi

swift - TextFields 不会使用展开函数 Swift 中的值进行更新

转载 作者:行者123 更新时间:2023-11-28 13:40:44 26 4
gpt4 key购买 nike

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

展开函数:

@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
}
}
TableView 中的

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

文本框委托(delegate)函数:

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

最佳答案

你可以简单地使用一个闭包来解决这种问题陈述,

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 的任何情况。

关于swift - TextFields 不会使用展开函数 Swift 中的值进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56074793/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com