2017-08-09 9 views
0

これは間違いなく簡単だと確信していますが、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?") 
    } 
} 
+0

をkeyboard.bringSubViewToFront:(のOneKeyが)キーボードがinputviewに追加した後、これをやってみてください。 –

+0

それはうまくいきませんでしたが、提案に感謝します。 –

+0

私はしばらくの間これを微調整し、.system UIButtonだけでうまくいきました。その後、MyButtonクラスに戻りました。最後に、上記のコードを貼り付けて、プロジェクトをきれいにしました。シミュレータのバグのように見えますか?とにかく、何かかわいそう。これのためのプロトコルは何ですか?質問全体を削除するか、これを他人に警告として残しますか? –

答えて

0

のCustomView Classsを削除します。私はちょうどこれが私の側にうまく働いた:

override func viewDidLoad() { 
    super.viewDidLoad() 
    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: .touchUpInside) 
    keyboard.addSubview(oneKey) 
    testTF.inputView = keyboard 
} 
@objc func clickMe(sender:Any) { 
    print("hey, why am I not being called?") 
} 

enter image description here

+0

Repadseのおかげで、Aadil、本当に感謝しています。それは他のすべてが失敗した場合に使用する回避策ですが、アップルはUIInputViewControllerサブクラスの使用を推奨しているので、私のコードがうまくいかない理由。 –

+0

サーあなたの感謝のためにありがとう!正確な解を知るためにもう一度試してみます。 –

関連する問題