0

I'm sure this is ridiculously easy, but I cannot get a simple example, with a UIInputViewController, to work. I've got two buttons, they show up, but tapping on them has no effect. In Googling, I found several questions exactly like this -- with no answers! I watched the WWDC 2017 video on the subject, but they sort of glossed over this one point, and their code works, but I couldn't see why mine doesn't.

The code (just proof of concept) is below, and any help would be hugely appreciated.

Michael

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?")  
    }
}  
BergQuester
  • 6,167
  • 27
  • 39
Michael Rogers
  • 1,318
  • 9
  • 22
  • keyboard.bringSubViewToFront:(oneKey) try doing this after keyboard added to inputview. – Aadil Ali Aug 09 '17 at 15:25
  • That didn't work, but thanks for the suggestion. – Michael Rogers Aug 09 '17 at 16:32
  • I tweaked this for a while, tried working just with a .system UIButton, and that worked. Then I went back to my MyButton class, that also worked. Finally, I pasted back the code above, cleaned the project, and it *still* works. Looks like a bug with the simulator, maybe? Something flakey, anyways. What's the protocol for this? Delete the entire question, or leave this up as a warning to to others? – Michael Rogers Aug 09 '17 at 21:59

2 Answers2

1

Remove CustomView Classs. I just did this worked fine at my side:

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

enter image description here

Aadil Ali
  • 351
  • 1
  • 15
  • Thanks for the repsonse, Aadil, I really appreciate it -- it's a workaround that I might use if all else fails, but since Apple recommends the use of UIInputViewController subclasses, I'd still like to know why my code doesn't work. – Michael Rogers Aug 09 '17 at 16:38
  • Sir Thank you for the appreciation! I will try it again to know the exact solution. – Aadil Ali Aug 09 '17 at 16:44
0

A keyboard extension will be executing in a separate process from the main process. Unless you explicitly set your debugging context to the extension, only the logging that you do in your host program will appear in the Xcode log window. Although you don't say so in the above, I suspect you started out trying to debug the host program and then changed the scheme to your keyboard, which resulted in it 'just working'

See the apple docs at https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionCreation.html#//apple_ref/doc/uid/TP40014214-CH5-SW8 for how to set up for a debugging session for extensions in Xcode.

Wil Macaulay
  • 507
  • 3
  • 12