我确信这非常简单,但我无法获得一个简单的例子,使用 UIInputViewController 来工作。我有两个按钮,它们会显示出来,但点击它们没有效果。在谷歌搜索中,我发现了几个与此完全相同的问题——没有答案!我观看了有关该主题的 WWDC 2017 视频,但他们有点掩盖了这一点,并且他们的代码有效,但我不明白为什么我的代码无效。
代码(只是概念证明)如下,任何帮助将不胜感激。
迈克尔
class ViewController: UIViewController {
@IBOutlet weak var testTF: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
testTF.inputView = CustomView(nibName:nil,bundle:nil).inputView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
class MyButton:UIButton {
init(frame: CGRect, title:String) {
super.init(frame: frame)
backgroundColor = UIColor.lightGray
isUserInteractionEnabled = true
setTitle(title, for: .normal)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class CustomView: UIInputViewController {
override init(nibName:String?, bundle:Bundle?) {
super.init(nibName:nibName, bundle:bundle)
let keyboard:UIView = UIView(frame: CGRect(x:0.0,y:0.0,width:768.0, height:240.0))
keyboard.backgroundColor = UIColor.red
let zeroKey:MyButton = MyButton(frame: CGRect(x:0.0,y:0.0,width:45.0,height:50.0), title:"0")
zeroKey.addTarget(self, action: #selector(clickMe(sender:)), for: .allEvents)
keyboard.addSubview(zeroKey)
let oneKey:UIButton = MyButton(frame: CGRect(x:50.0,y:0.0,width:45.0,height:50.0), title:"1")
oneKey.addTarget(self, action: #selector(clickMe(sender:)), for: .allEvents)
keyboard.addSubview(oneKey)
self.inputView?.backgroundColor = UIColor.blue
self.inputView?.frame = CGRect(x:0.0,y:0.0,width:UIScreen.main.bounds.width, height:240.0)
self.inputView?.isUserInteractionEnabled = true
self.inputView?.addSubview(keyboard)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func clickMe(sender:Any) {
print("hey, why am I not being called?")
}
}