Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIInputViewController(自定义键盘)-未触发UIButton操作-为什么,为什么?_Ios_Custom Keyboard_Uiinputviewcontroller - Fatal编程技术网

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

Ios UIInputViewController(自定义键盘)-未触发UIButton操作-为什么,为什么?,ios,custom-keyboard,uiinputviewcontroller,Ios,Custom Keyboard,Uiinputviewcontroller,我相信这是非常简单的,但我无法得到一个简单的例子,一个UIInputViewController,工作。我有两个按钮,它们会出现,但点击它们没有效果。在谷歌搜索中,我发现了几个与此完全相同的问题——没有答案!我看了WWDC 2017关于这个主题的视频,但他们有点掩盖了这一点,他们的代码可以工作,但我不明白为什么我的代码不能 下面的代码只是概念的证明,任何帮助都将不胜感激 迈克尔 class ViewController: UIViewController { @IBOutlet w

我相信这是非常简单的,但我无法得到一个简单的例子,一个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?")  
    }
}  

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

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


有关如何在Xcode中为扩展设置调试会话的信息,请参阅位于的apple文档

keyboard.bringSubViewToFront:oneKey在键盘添加到inputview后尝试执行此操作。这不起作用,但感谢您的建议。我对此进行了一段时间的调整,尝试只使用.system UIButton,结果成功了。然后我又回到了我的按钮课,这也很有效。最后,我粘贴了上面的代码,清理了项目,它仍然有效。可能是模拟器上的一个bug吧?不管怎么说,有点像雪花。这是什么协议?删除整个问题,或者将此作为对其他人的警告?谢谢你的回复,Aadil,我真的很感激-如果其他方法都失败了,这是一个我可能会使用的解决方法,但是由于苹果建议使用UIInputViewController子类,我仍然想知道为什么我的代码不起作用。先生,谢谢你的赞赏!我将再试一次,以了解确切的解决方案。