我敢肯定,这是非常容易的,但我无法得到一个简单的例子,用UIInputViewController工作。我有两个按钮,它们出现,但敲击它们没有效果。在谷歌搜索中,我发现了几个完全如此的问题 - 没有答案!我观看了关于这个主题的WWDC 2017视频,但是他们对这一点有所掩盖,并且他们的代码有效,但我不明白为什么我的代码不会。UIInputViewController(自定义键盘) - UIButton操作未被触发 - 为什么,为什么?
代码(只是概念验证)在下面,任何帮助将非常感激。
迈克尔
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?")
}
}
keyboard.bringSubViewToFront:(一键)尝试这样做后,键盘加入inputview。 –
这没有奏效,但谢谢你的建议。 –
我调整了这一段时间,试图与一个.system UIButton一起工作,而且工作。然后我回到我的MyButton类,这也工作。最后,我粘贴了上面的代码,清理了该项目,并且它仍然可以工作。看起来像模拟器的错误,也许?无论如何,这是一件有趣的事情。这是什么协议?删除整个问题,或将其作为警告留给别人? –