UIInputViewController(自定义键盘) - 未触发UIButton操作 - 为什么,为什么?

时间:2017-08-09 15:20:08

标签: ios custom-keyboard uiinputviewcontroller

我确信这非常容易,但我无法通过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?")  
    }
}  

2 个答案:

答案 0 :(得分:0)

删除CustomView类。我刚刚在我这边做得很好:

    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

答案 1 :(得分:0)

键盘扩展名将在与主进程不同的进程中执行。除非您为扩展名明确设置调试上下文,否则Xcode日志窗口中只会显示您在主机程序中执行的日志记录。尽管您在上面没有这么说,但我怀疑您是开始尝试调试主机程序,然后将方案更改为键盘,这导致它“可以正常工作”

有关如何设置Xcode扩展调试会话的信息,请参见https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionCreation.html#//apple_ref/doc/uid/TP40014214-CH5-SW8上的Apple文档。