0
votes

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?

1
Have you set delegate for each UITextField?Dharmesh Kheni
It looks like you are setting the delegate for activeField before assigning it to anything. Try assigning it each time textFieldDidBeginEditing or set for each of your 4 textFields in viewdidLoad.Magnas
@Magnas Setting the delegate for each text field in awakeFromNib() and removing activeField?.inputView = self from textFieldDidBeginEditing allowed me to input into the text field. Thanks! Keyboard still closes and reappears.Amy Fang
@DharmeshKheni when I tried it, it makes it to textFieldDidBeginEditing function but the keyboard still closes and reappears. Nevertheless, at least now I can input into my text fields. Thanks!Amy Fang
can you share any demo project where I can check this problem? @AmyFangDharmesh Kheni

1 Answers

0
votes

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