我确信这非常简单,但是我不能让一个简单的例子,使用一个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?")
}
} 发布于 2017-08-09 23:49:14
删除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?")
}

发布于 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上的苹果文档。
https://stackoverflow.com/questions/45594696
复制相似问题