簡體   English   中英

在向包含UITextFields的UITableView單元格中輸入文本時控制鍵盤

[英]Control of keyboard when entering text into UITableView cells containing UITextFields

我有一個UITableView,其中的自定義單元格包含兩個用於文本輸入的UITextFields。 我希望鍵盤一直保持在原位置,直到完成文本輸入為止,但是當敲擊第二個textField時,鍵盤消失了,我必須再按一下以將其取回。 我了解第一次敲擊會觸發“ resignFirstResponder”消息,但是如何使鍵盤保持在原位? 嘗試設置tableScrollView keyboardDissmissMode,但沒有區別。 我實現了'textFieldDidEndEditing'來存儲輸入的文本,並將視圖控制器聲明為texField的委托.Am在iOS 12上使用Swift 4。

這是自定義單元格的代碼:

類ChoreoCell:UITableViewCell,UITextFieldDelegate {var inputText:String?

@IBOutlet weak var countText: UITextField! {
    didSet {
        countText.sizeToFit()
        countText.text = inputText
    }
}

@IBOutlet weak var stepText: UITextField! {
    didSet {
        stepText.sizeToFit()
        stepText.text = inputText
    }
} 

}

...這是我在tableViewController中使用的代碼:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell( withIdentifier:Storyboard.CellIdentifier, for: indexPath) as? ChoreoCell
    if (self.choreography.count == 0 ) {
        let myCountSteps = CountSteps(counts:"", steps:"" )
        for _ in 0...Storyboard.InitialRows-1 {
            self.choreography.append(myCountSteps)
        }
    }
    cell!.countText.text = self.choreography[indexPath.row].counts
    cell!.stepText.text = self.choreography[indexPath.row].steps
    cell!.countText.delegate = self
    cell!.stepText.delegate = self
    return cell!
}

func textFieldDidEndEditing(_ textField: UITextField) {
    guard let txt : String = textField.text else {
        print ("input error for text, editing ended but no text received")
        return
    }
    var v:UIView = textField
    repeat { v = v.superview!} while !(v is UITableViewCell)
    if let cell = v as? ChoreoCell {
        guard let ip = self.tableView.indexPath(for: cell) else {
            print("Error identifying index path for \(cell)")
            return
        }            
        switch (textField) {
            case cell.countText:
                let myCountSteps = CountSteps(counts:txt, steps:cell.stepText.text! )
                self.choreography[ip.row] = myCountSteps
            case cell.stepText:
                let myCountSteps = CountSteps(counts:cell.countText.text!, steps:txt )
                self.choreography[ip.row] = myCountSteps
            default:
                print ("Unexpected textField case")
        }
        self.danceDoc?.choreography[ip.row] = self.choreography[ip.row]
        self.danceDoc?.updateChangeCount(.done)
        return
    } else {
        print ("Error identifying cell from text entry")
        return
    }
} 

如何確定要輸入哪個textField來結束當前textField的編輯,以便可以將其分配為第一響應者狀態並防止鍵盤消失?

您可能具有與此類似的代碼,無論如何,這都會導致鍵盤被解雇:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()            
    return true
}

要解決此問題-您需要以編程方式激活下一個“文本字段”:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()       
    self.nextTextField.becomeFirstResponder() // Add this line
    return true
}

您可能需要進行其他檢查,因為對於最后一個字段,您實際上可能需要關閉鍵盤。

不能100%確定它是否不會影響其他功能,但我相信甚至可以省略textField.resignFirstResponder()行(以防我們希望鍵盤保留在屏幕上),否則可能會導致鍵盤產生一些怪異的動畫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM