0

我确信这非常简单,但我无法获得一个简单的例子,使用 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?")  
    }
}  
4

2 回答 2

1

删除 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?")
}

在此处输入图像描述

于 2017-08-09T15:49:13.890 回答
0

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

有关如何设置Xcode 中扩展的调试会话。

于 2018-11-09T23:17:10.947 回答