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?
UITextField
? – Dharmesh Kheni