【问题标题】:How to goto next textfield when using one common textfield for all textfield?对所有文本字段使用一个通用文本字段时如何转到下一个文本字段?
【发布时间】:2021-09-23 09:39:24
【问题描述】:

我在 tableview 的 storyboard 中放置了一个 UITextField,并使用代码创建了多个文本字段(4 个)。

我的问题是在键盘上按回车键时我无法转到第二个 uitextfield。我在表格视图单元格中使用了以下代码

func textFieldShouldReturn(textField: UITextField) -> Bool {
    let nextTag: NSInteger = textField.tag + 1

    let nextResponder: UIResponder = textField.superview!.viewWithTag(nextTag)!
    if (nextResponder != nil) {

        nextResponder.becomeFirstResponder()
    } else {
      
        textField.resignFirstResponder()
    }
    return false 
}

【问题讨论】:

  • 您是否在属性检查器中检查了表格视图的 UItextfield 的标签?
  • 你说 "... 在 tableview 中,并创建多个文本字段(4 个)..." -- 那么,你的意思是你有一个UITextField在一个单元格中,并且您的表格中有 4 行?或者,您的意思是 单个单元格中有 4 个UITextFields?
  • @DonMag 我使用情节提要在 tableview 单元格上放置了一个文本字段,并创建 4 行并将该文本字段添加到那里。
  • "... 使用情节提要的 tableview 单元格并创建 4 行..." -- 好的,这就是你的问题。在您的代码中:textField.superview!.viewWithTag(nextTag)!textField.superview该单元格的 contentView。因此,它不会在不同的单元格中找到不同的视图。您可能应该使用闭包,以便您的代码可以告诉 tableView 控制器将下一行的文本字段设置为第一响应者。

标签: ios swift uitableview uitextfield


【解决方案1】:

首先在cellForRowAtIndexPath方法中设置当前行的textField标签。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
        
        cell.textField.tag = indexPath.row

    return cell
    }

然后在textFieldShouldReturn方法中获取下一个单元格并设置下一个单元格文本字段firstResponder

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    let nexttag = textField.tag + 1
    if nexttag < tableView.numberOfRows(inSection: 0) {
        let nextIndexPath = IndexPath(row: nexttag, section: 0)
        if let cell = tableView.cellForRow(at: nextIndexPath) as? MyCell{
            cell.textfield.becomeFirstResponder()
            tableView.scrollToRow(at: nextIndexPath, at: .none, animated: true)
        }
    } else {
        textField.resignFirstResponder()
    }
    return false
}

【讨论】:

    【解决方案2】:

    您的表格中有 4 行,其中每个单元格都有一个 UITextField

    所以你正在尝试使用这段代码:

    textField.superview!.viewWithTag(nextTag)!
    

    不起作用,因为textField.superview 是包含该文本字段的单元格的contentView

    一种方法是使用闭包,让您的单元格告诉控制器用户点击了返回,然后让控制器处理将下一行的文本字段设置为第一响应者。

    这是一个非常简单的示例,说明您可能希望如何实现它:

    class ResponderCell: UITableViewCell, UITextFieldDelegate {
        
        @IBOutlet var textField: UITextField!
        
        var returnClosure: ((String, UITableViewCell)->())?
        
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    
            textField.resignFirstResponder()
            
            // tell our table view controller
            returnClosure?(textField.text ?? "", self)
            
            return false
    
        }
        
    }
    
    class ResponderTableViewController: UITableViewController {
        var myData: [String] = [
            "One",
            "Two",
            "Three",
            "Four",
        ]
        
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return myData.count
        }
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let c = tableView.dequeueReusableCell(withIdentifier: "rCell", for: indexPath) as! ResponderCell
            
            c.textField.text = myData[indexPath.row]
            
            c.returnClosure = { [weak self, weak tableView] str, c in
                // make sure we're safely accessing these objects
                guard let self = self,
                      let tableView = tableView,
                      let pth = tableView.indexPath(for: c)
                else {
                    return
                }
    
                // update data array with textField's text
                self.myData[pth.row] = str
    
                // increment the row
                let nextIndexPath = IndexPath(row: pth.row + 1, section: pth.section)
    
                // if we can get the next cell...
                if let nextCell = self.tableView.cellForRow(at: nextIndexPath) as? ResponderCell {
                    // set it's textField to first responder
                    nextCell.textField.becomeFirstResponder()
                }
            }
    
            return c
        }
        
    }
    

    【讨论】:

    • 这行得通,你能告诉我我们如何使用 Rxswift 做到这一点吗?
    猜你喜欢
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多