TextFields不会使用展开函数Swift中的值更新 [英] TextFields don't get updated with values in unwind function 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 unwind
function, but textfields
don't get updated. When printing the value it prints correctly on unwinding, so unwind
should be set correctly, but it just don't get displayed in it's textfield
. I also tried prepare(for unwind:
in TableViewController
but 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
}
}
准备放松: 文本字段委托函数:
您可以简单地使用 You can simply use a 上面的代码是通用的,可以在您要打开 The above code is generic and will work in every case from where you want to open 这篇关于TextFields不会使用展开函数Swift中的值更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!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
解决此类问题陈述,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
的任何位置使用.TableViewController
.