ios - TextField 在其他视图中通过协议更改值,如果用户更改 textField 输入如何存储值
问题描述
我的单元格自定义类中的 textField 有问题。
功能:当用户选择行时,他转到tipsView,在那里他可以选择一些Tips,我在这里实现了协议,保存了那个值。它工作正常,但是,当用户将自己的信息放入 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 方法上。
推荐阅读
- webrtc - Coturn 服务器给了 code=701 TURN allocate request timed out
- flutter - 如果我使用 .tostring 或错误消息颤振设置变量,则返回 Null
- javascript - 需要帮助解决承诺并分配返回值
- javascript - 如何在我的反应应用程序中删除这些 html 标签
- automated-tests - Protractor-Flake 在重试后在所有实例中运行相同的脚本,而不是仅使用一个实例
- flutter - GetX Streams 中的 .addError 问题
- sql - SQL - 获取只有进入和退出日期的 MAX COUNT 访问峰值
- apache-kafka - 将 Kafka Connect Avro 转换器从 5.2.1 升级到 6.2.0 但没有数据进入
- javascript - 在平面列表中每 3 个项目添加一个分隔符 - React Native
- mojibake - 是什么导致了这个 Mojibake 中的 ГѓВ 模式?