簡體   English   中英

使用UIButtons在自定義鍵盤中全部輸入到UITextFields中,重新出現鍵盤,而不輸入文本

[英]Using UIButtons to input into UITextFields all in a custom keyboard, keyboard reappears, not inputting text

我正在嘗試制作一個自定義的iOS鍵盤,所有這些都包含在BibleKeyboardView.swiftBibleKeyboardView.xib文件中。 該視圖的一部分包含多個UITextField,我希望通過數字按鈕輸入它們。 但是,當我單擊任意一個UITextField時,鍵盤將關閉,然后重新出現,光標將永遠不會停留在UITextField中,並且UIButton不會執行任何操作。

鍵盤的Gif消失/重新出現的問題

我試過設置每個UITextField的inputView = self但是只能保持鍵盤關閉。 我還將每個數字按鈕設置為情節提要面板右側菜單中的鍵盤鍵。

這是我的代碼,但是當我嘗試運行它時, activeField為nil並引發Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value錯誤Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value 該代碼永遠不會將其傳遞給textFieldDidBeginEditing()因為該打印語句不會運行(基於如何將文本添加到活動UITextField )。

activeField為nil錯誤屏幕截圖

class BibleKeyboardView: UIView, UITextFieldDelegate {

    @IBOutlet weak var chapterA: UITextField!
    @IBOutlet weak var verseA: UITextField!
    @IBOutlet weak var chapterB: UITextField!
    @IBOutlet weak var verseB: UITextField!

    var activeField: UITextField?

    override func awakeFromNib() {
        super.awakeFromNib()
        activeField?.delegate = self
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        activeField = textField
        activeField?.inputView = self
        print("made active field")
    }

    @IBAction func numBtnTapped(_ sender: UIButton) {
        activeField!.text = activeField!.text! + (sender.titleLabel?.text!)!
}

夢想是當我使用編碼的數字鍵盤點擊時,我可以在每個UITextField中輸入數字。 當我單擊UITextField時,為什么鍵盤會不斷消失並重新出現? 為什么textFieldDidBeginEditing沒有運行?

最后的答案是:在awakeFromNib()為每個UITextField設置委托和inputView。 另外,似乎鍵盤關閉/重新出現的問題僅發生在iPad模擬器上,但是當我在實際的iPad上運行它時,它消失了。

class BibleKeyboardView: UIView, UITextFieldDelegate {

    @IBOutlet weak var chapterA: UITextField!
    @IBOutlet weak var verseA: UITextField!
    @IBOutlet weak var chapterB: UITextField!
    @IBOutlet weak var verseB: UITextField!

   var activeField: UITextField?

    override func awakeFromNib() {
        super.awakeFromNib()

        chapterA.delegate = self
        chapterA.inputView = self

        verseA.delegate = self
        verseA.inputView = self

        chapterB.delegate = self
        chapterB.inputView = self

        verseB.delegate = self
        verseB.inputView = self
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        activeField = textField
    }

    @IBAction func numBtnTapped(_ sender: UIButton) {
        activeField!.text = activeField!.text! + (sender.titleLabel?.text!)!
    }
}

暫無
暫無

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

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