How to input text using the buttons of an in-app custom keyboard

后端 未结 2 648
刺人心
刺人心 2021-01-17 23:53

I\'ve made an in-app custom keyboard that replaces the system keyboard and pops up when I tap inside a UITextField.

Here is my code:



        
2条回答
  •  -上瘾入骨i
    2021-01-18 00:26

    I imagine something like this:

    A new function to handle button event

    func updateTextfield(sender: UIButton) {
        textField.text = (textField.text ?? "") + (sender.titleForState(.Normal) ?? "")
    }
    

    And after init your custom keyboard, register the buttons:

    myCustomKeyboard.subviews
        .filter { $0 as? UIButton != nil } // Keep the buttons only
        .forEach { ($0 as! UIButton).addTarget(self, action: "updateTextfield", forControlEvents: .TouchUpInside)}
    

提交回复
热议问题