【问题标题】:UIInputViewController (Custom Keyboard) - UIButton action not being triggered - Why, Oh Why?UIInputViewController(自定义键盘) - UIButton 动作没有被触发 - 为什么,哦,为什么?
【发布时间】:2018-01-17 13:55:57
【问题描述】:

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

【问题讨论】:

  • keyboard.bringSubViewToFront:(oneKey) 在将键盘添加到 inputview 后尝试执行此操作。
  • 这不起作用,但感谢您的建议。
  • 我对此进行了一段时间的调整,尝试仅使用 .system UIButton,并且成功了。然后我回到我的 MyButton 课程,这也有效。最后,我将上面的代码粘贴回来,清理了项目,它仍然可以工作。看起来像模拟器的错误,也许?无论如何,有些古怪。这是什么协议?删除整个问题,还是留给其他人作为警告?

标签: ios custom-keyboard uiinputviewcontroller


【解决方案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?")
}

【讨论】:

  • 感谢您的回复,Aadil,我真的很感激 - 如果一切都失败了,我可能会使用它,但由于 Apple 建议使用 UIInputViewController 子类,我仍然想知道为什么我的代码不起作用。
  • 先生,谢谢您的赞赏!我会再试一次以知道确切的解决方案。
【解决方案2】:

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

请参阅https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionCreation.html#//apple_ref/doc/uid/TP40014214-CH5-SW8 上的苹果文档,了解如何为 Xcode 中的扩展设置调试会话。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-08
    相关资源
    最近更新 更多