gpt4 book ai didi

ios - 使用UIButtons在自定义键盘中输入UITextFields,键盘重新出现,而不是输入文本

转载 作者:行者123 更新时间:2023-11-29 06:00:21 24 4
gpt4 key购买 nike

我正在尝试制作一个自定义 iOS 键盘,所有内容都包含在 BibleKeyboardView.swiftBibleKeyboardView.xib 文件中。 View 的一部分包含多个 UITextFields,我想通过数字按钮输入它们。但是,当我单击任何 UITextFields 时,键盘关闭然后重新出现,光标永远不会停留在 UITextField 中,并且 UIButtons 不执行任何操作。

Gif of the keyboard disappearing/reappearing issue

我尝试设置每个 UITextField 的 inputView = self 但这只会让键盘保持关闭状态。我还在 Storyboard右侧菜单中将每个数字按钮设置为键盘键。

这是我的代码,但是当我尝试运行它时,activeField 为 nil 并抛出 Thread 1: Fatal error: Unexpectedly find nil while unwrapping anOptional value 错误。该代码永远不会到达 textFieldDidBeginEditing() 因为该 print 语句不会运行(基于 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!)!
}

我的梦想是,当我使用我编码的数字键盘点击时,我可以在每个 UITextField 中输入数字。 为什么当我点击 UITextField 时键盘不断消失又重新出现?为什么 textFieldDidBeginEditing 没有运行?

最佳答案

最后的答案:在awakeFromNib()中为每个UITextField设置委托(delegate)和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!)!
}
}

关于ios - 使用UIButtons在自定义键盘中输入UITextFields,键盘重新出现,而不是输入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54741492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com