简体   繁体   English

UITextField inputView-未触发IBAction

[英]UITextField inputView - IBActions not fired

I want to replace the default keyboard of a UITextField with a custom keyboard. 我想用自定义键盘替换UITextField的默认键盘。 So I created a new subclass of a UIViewController with a xib-file (the other way like creating both files seperately and setting the File's Owner doesn't work either). 因此,我创建了带有xib文件的UIViewController的新子类(另一种方法是分别创建两个文件和设置文件的所有者也不起作用)。

Then I added a button to the KeyboardView and connected it to an IBAction . 然后,我向KeyboardView添加了一个按钮,并将其连接到IBAction After that I set the textfields inputView to the new view like this: 之后,将文本字段inputView设置为新视图,如下所示:

override func viewDidLoad() {
    let keyboardVC = KeyboardViewController()
    textField.inputView = keyboardVC.view
    keyboardVC.view.autoresizingMask = .FlexibleHeight
    keyboardVC.delegate = textField
}

It's working and the custom keyboard shows up, but if I touch the button, the IBAction is not called. 它可以正常工作,并且显示自定义键盘,但是如果我触摸该按钮,则不会调用IBAction。 What's the problem in my setup? 我的设置有什么问题? (I checked some examples and they all do it the same way). (我检查了一些示例,它们都以相同的方式进行操作)。

UPDATE: 更新:

I now removed the ViewController and subclassed the UIView. 现在,我删除了ViewController并将其子类化为UIView。 Now the actions are working. 现在这些动作正在起作用。 Why isn't it working with a ViewController? 为什么它不能与ViewController一起使用?

Since no one holds the UIViewController - there is no reference to it after the viewDidLoad() ended, it is released from the memory. 由于没有人持有UIViewControllerviewDidLoad()结束后没有对它的引用,因此将其从内存中释放。 When the button is pressed, the view controller that should response to the action is not exist -> you are holding only the view of the view controller as the textField.inputView . 当按下按钮时,不应该响应该操作的视图控制器不存在->您仅将视图控制器的视图作为textField.inputView持有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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