首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIInputViewController (自定义键盘)-未触发UIButton操作-为什么,哦,为什么?

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

Stack Overflow用户
提问于 2017-08-09 23:20:08
回答 2查看 1.6K关注 0票数 0

我确信这非常简单,但是我不能让一个简单的例子,使用一个UIInputViewController来工作。我有两个按钮,它们出现了,但点击它们没有任何效果。在谷歌搜索中,我发现了几个与此完全相似的问题--没有答案!我看了WWDC 2017年关于这个主题的视频,但他们有点忽略了这一点,他们的代码可以工作,但我不明白为什么我的代码不能。

代码(只是概念的证明)在下面,任何帮助都将不胜感激。

迈克尔

代码语言:javascript
复制
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?")  
    }
}  
EN

回答 2

Stack Overflow用户

发布于 2017-08-09 23:49:14

删除CustomView类。我只是这样做了,在我身边工作得很好:

代码语言:javascript
复制
    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?")
}

票数 1
EN

Stack Overflow用户

发布于 2018-11-10 07:17:11

键盘扩展将在独立于主进程的单独进程中执行。除非您显式地将调试上下文设置为扩展,否则Xcode日志窗口中将只显示您在宿主程序中执行的日志记录。尽管你在上面没有这样说,但我怀疑你一开始是试图调试主机程序,然后改变了键盘的设计方案,结果就是它“就能工作”

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45594696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档