【发布时间】:2018-01-17 13:55:57
【问题描述】:
我确信这非常简单,但我无法获得一个简单的示例,使用 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?")
}
}
【问题讨论】:
-
keyboard.bringSubViewToFront:(oneKey) 在将键盘添加到 inputview 后尝试执行此操作。
-
这不起作用,但感谢您的建议。
-
我对此进行了一段时间的调整,尝试仅使用 .system UIButton,并且成功了。然后我回到我的 MyButton 课程,这也有效。最后,我将上面的代码粘贴回来,清理了项目,它仍然可以工作。看起来像模拟器的错误,也许?无论如何,有些古怪。这是什么协议?删除整个问题,还是留给其他人作为警告?
标签: ios custom-keyboard uiinputviewcontroller