首页 > 解决方案 > 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)
                
            }
        }
        
    }

标签: iosswiftprotocolstextfield

解决方案


不确定我是否完全理解您的要求,但您可以遵守 UITextFieldDelegate 并将值保存在 shouldChangeCharactersIn 方法上。


推荐阅读