简体   繁体   English

使用UIButtons在自定义键盘中全部输入到UITextFields中,重新出现键盘,而不输入文本

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

I'm trying to make a custom iOS keyboard, all contained in the BibleKeyboardView.swift and BibleKeyboardView.xib file. 我正在尝试制作一个自定义的iOS键盘,所有这些都包含在BibleKeyboardView.swiftBibleKeyboardView.xib文件中。 Part of the view contains multiple UITextFields which I want to be inputted by the number buttons. 该视图的一部分包含多个UITextField,我希望通过数字按钮输入它们。 However, when I click on any of the UITextFields, the keyboard closes and then reappear, the cursor never stays in the UITextField, and the UIButtons don't do anything. 但是,当我单击任意一个UITextField时,键盘将关闭,然后重新出现,光标将永远不会停留在UITextField中,并且UIButton不会执行任何操作。

Gif of the keyboard disappearing/reappearing issue 键盘的Gif消失/重新出现的问题

I've tried setting each of the UITextField's inputView = self but that only kept the keyboard closed. 我试过设置每个UITextField的inputView = self但是只能保持键盘关闭。 I have also set each number button as a keyboard key in the storyboard right side menu. 我还将每个数字按钮设置为情节提要面板右侧菜单中的键盘键。

This is my code, but when I try running it, activeField is nil and throws an Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value error. 这是我的代码,但是当我尝试运行它时, 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 The code never makes it to textFieldDidBeginEditing() because that print statement doesn't run (based on How to add text to an active UITextField ). 该代码永远不会将其传递给textFieldDidBeginEditing()因为该打印语句不会运行(基于如何将文本添加到活动UITextField )。

activeField is nil error screenshot 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!)!
}

The dream is that I can input numbers into each UITextField when tapping on using the numeric pad that I coded. 梦想是当我使用编码的数字键盘点击时,我可以在每个UITextField中输入数字。 Why does the keyboard keep disappearing and reappearing when I click on the UITextField? 当我单击UITextField时,为什么键盘会不断消失并重新出现? Why is textFieldDidBeginEditing not running? 为什么textFieldDidBeginEditing没有运行?

The answer in the end: setting the delegate and inputView for each UITextField within awakeFromNib() . 最后的答案是:在awakeFromNib()为每个UITextField设置委托和inputView。 Also, it seems the keyboard closing/reappearing issue only happens on the iPad simulator, but when I run it on an actual iPad, it goes away. 另外,似乎键盘关闭/重新出现的问题仅发生在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