これは間違いなく簡単だと確信していますが、UIInputViewControllerを使って簡単な例を得ることはできません。私は2つのボタンを持っている、彼らは表示されますが、それらをタップしても効果はありません。グーグルでは、このようないくつかの質問が見つかりました。答えはありません。私は主題に関するWWDC2017ビデオを見ましたが、この1つの点で一見光沢があり、コードは機能しますが、なぜ私が見ることができませんでした。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:(のOneKeyが)キーボードがinputviewに追加した後、これをやってみてください。 –
それはうまくいきませんでしたが、提案に感謝します。 –
私はしばらくの間これを微調整し、.system UIButtonだけでうまくいきました。その後、MyButtonクラスに戻りました。最後に、上記のコードを貼り付けて、プロジェクトをきれいにしました。シミュレータのバグのように見えますか?とにかく、何かかわいそう。これのためのプロトコルは何ですか?質問全体を削除するか、これを他人に警告として残しますか? –