简体   繁体   中英

UITextField inputView - IBActions not fired

I want to replace the default keyboard of a UITextField with a custom keyboard. 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).

Then I added a button to the KeyboardView and connected it to an IBAction . After that I set the textfields inputView to the new view like this:

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. 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. Now the actions are working. Why isn't it working with a ViewController?

Since no one holds the UIViewController - there is no reference to it after the viewDidLoad() ended, it is released from the memory. 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 .

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