TextField 在其他视图中通过协议更改值,如果用户更改 textField 输入如何存储值
TextField changes value by protocol in other view, how to store value if user changes textField input
我的自定义 class 单元格中的 textField 有问题。
功能:
当用户 select 行时,他转到 tipsView,在那里他可以选择一些提示,我在这里实现了协议,它保存了那个值。
它工作正常,但是,当用户将自己的信息放入 textField 并在不选择 smth 的情况下转到 tipsView 时,他将从协议获得旧值。
我在第一个视图中有全局变量,它保存了 tipsView 中的协议实现。
但我无法重置此值或使用 if else 语句,因为我没有看到任何好的解决方案,如果你能告诉我去哪里,我将很乐意阅读并向你学习!谢谢!
class Example: UITableViewController {
var selectedCellIdentifier: String = ""
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "tasking") as! tasking
cell.textField.text = selectedCellIdentifier
//
// if cell.textField.text != nil {
//
// selectedCellIdentifier = cell.textField.text!
//
//
// } else {
//
// cell.textField.text = selectedCellIdentifier
// }
// if selectedCellIdentifier != cell.textField.text && cell.textField.text != "" {
// cell.textField.text = cell.textField.text
// selectedCellIdentifier = ""
// } else {
// cell.textField.text = selectedCellIdentifier
// }
return cell
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == segues.goToTips {
let destinationVC = segue.destination as! TaskTips
destinationVC.delegate = self
if let taskName = tasking().textField?.text {
destinationVC.selectedCellValue = taskName
}
}
}
}
extension Example: ChildViewControllerDelegate{
func textTips(string: String) {
selectedCellIdentifier = string
}
第二个视图
protocol ChildViewControllerDelegate {
func textTips(string: String)
}
class tipsView: UITableViewController {
public var delegate: ChildViewControllerDelegate!
var selectedCellValue = ""
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let indexPath = tableView.indexPathForSelectedRow {
if let name = cells[indexPath.section].cellName?[indexPath.row] {
print(selectedCellValue)
// if name != selectedCellValue {
// self.delegate?.textTips(string: name)
// } else if name == "" {
// self.delegate?.textTips(string: selectedCellValue)
// } else if name == selectedCellValue {
// self.delegate.textTips(string: selectedCellValue)
// }
if name != "" {
self.delegate?.textTips(string: name)
} else {
self.delegate?.textTips(string: selectedCellValue)
}
print(name)
}
tableView.reloadData()
self.navigationController?.popViewController(animated: true)
}
}
}
不确定我是否完全理解您的要求,但您可以遵循 UITextFieldDelegate 并将值保存在 shouldChangeCharactersIn 方法中。
这是我的解决方案!
创建了一个数组,并查看我在文本字段中的值是否包含在数组中,工作完成!
func textFieldDidEndEditing(_ textField: UITextField) {
let text = selectedCellIdentifier
if cellTips().cellArray.contains(text) {
textField.text = text
} else {
textField.text = textField.text
}
selectedCellIdentifier = ""
}
我的自定义 class 单元格中的 textField 有问题。
功能: 当用户 select 行时,他转到 tipsView,在那里他可以选择一些提示,我在这里实现了协议,它保存了那个值。 它工作正常,但是,当用户将自己的信息放入 textField 并在不选择 smth 的情况下转到 tipsView 时,他将从协议获得旧值。
我在第一个视图中有全局变量,它保存了 tipsView 中的协议实现。 但我无法重置此值或使用 if else 语句,因为我没有看到任何好的解决方案,如果你能告诉我去哪里,我将很乐意阅读并向你学习!谢谢!
class Example: UITableViewController {
var selectedCellIdentifier: String = ""
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "tasking") as! tasking
cell.textField.text = selectedCellIdentifier
//
// if cell.textField.text != nil {
//
// selectedCellIdentifier = cell.textField.text!
//
//
// } else {
//
// cell.textField.text = selectedCellIdentifier
// }
// if selectedCellIdentifier != cell.textField.text && cell.textField.text != "" {
// cell.textField.text = cell.textField.text
// selectedCellIdentifier = ""
// } else {
// cell.textField.text = selectedCellIdentifier
// }
return cell
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == segues.goToTips {
let destinationVC = segue.destination as! TaskTips
destinationVC.delegate = self
if let taskName = tasking().textField?.text {
destinationVC.selectedCellValue = taskName
}
}
}
}
extension Example: ChildViewControllerDelegate{
func textTips(string: String) {
selectedCellIdentifier = string
}
第二个视图
protocol ChildViewControllerDelegate {
func textTips(string: String)
}
class tipsView: UITableViewController {
public var delegate: ChildViewControllerDelegate!
var selectedCellValue = ""
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let indexPath = tableView.indexPathForSelectedRow {
if let name = cells[indexPath.section].cellName?[indexPath.row] {
print(selectedCellValue)
// if name != selectedCellValue {
// self.delegate?.textTips(string: name)
// } else if name == "" {
// self.delegate?.textTips(string: selectedCellValue)
// } else if name == selectedCellValue {
// self.delegate.textTips(string: selectedCellValue)
// }
if name != "" {
self.delegate?.textTips(string: name)
} else {
self.delegate?.textTips(string: selectedCellValue)
}
print(name)
}
tableView.reloadData()
self.navigationController?.popViewController(animated: true)
}
}
}
不确定我是否完全理解您的要求,但您可以遵循 UITextFieldDelegate 并将值保存在 shouldChangeCharactersIn 方法中。
这是我的解决方案!
创建了一个数组,并查看我在文本字段中的值是否包含在数组中,工作完成!
func textFieldDidEndEditing(_ textField: UITextField) {
let text = selectedCellIdentifier
if cellTips().cellArray.contains(text) {
textField.text = text
} else {
textField.text = textField.text
}
selectedCellIdentifier = ""
}