简体   繁体   中英

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. Part of the view contains multiple UITextFields which I want to be inputted by the number buttons. 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.

Gif of the keyboard disappearing/reappearing issue

I've tried setting each of the UITextField's inputView = self but that only kept the keyboard closed. 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. The code never makes it to textFieldDidBeginEditing() because that print statement doesn't run (based on How to add text to an active UITextField ).

activeField is nil error screenshot

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. Why does the keyboard keep disappearing and reappearing when I click on the UITextField? Why is textFieldDidBeginEditing not running?

The answer in the end: setting the delegate and inputView for each UITextField within awakeFromNib() . 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.

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!)!
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM