1

I'm new to iOS programming. I'm developing a custom keyboard with a custom nib file for it's view. How can I add target for buttons in the nib file without connect them manually to my UIInputViewController? I tried this code but doesn't works!

class LatinKeyboard: UIInputViewController {

override func viewDidLoad() {
    super.viewDidLoad();
    let nib = UINib(nibName: "KeyboardView", bundle: nil);
    let objects = nib.instantiateWithOwner(self, options: nil)
    view = objects[0] as UIView;        

    for subview in view.subviews {
        if(subview is UIButton){
            var key = subview as UIButton;
            key.addTarget(self, action: "keyPressed:", forControlEvents: .TouchUpInside);
        }
    }


}



func keyPressed(sender: AnyObject?) {
    let button = sender as UIButton
    let title = button.titleForState(.Normal)
    (textDocumentProxy as UIKeyInput).insertText(title!)
}

}

Mansour
  • 445
  • 1
  • 4
  • 14
  • What is not working? Is it compiling? In Xcode 7.0.1 this doesn't compile for me: let button = sender as UIButton. – Mike Taverne Oct 17 '15 at 16:29
  • It loads KeyboardView nib file and shows the loaded view. KeyboardView contains some buttons but i cannot access them as subviews of view. I don't know how i can access them and add target for them without set outlet for each button separately. – Mansour Oct 17 '15 at 17:59
  • When you loop through view.subviews, how many subviews are there and can you tell what type they are? – Mike Taverne Oct 17 '15 at 18:14
  • This article looks like a great tutorial on custom keyboards. I think it is a better approach that will avoid the kind of issue you are having. http://code.tutsplus.com/tutorials/ios-8-creating-a-custom-keyboard-in-swift--cms-22344 – Mike Taverne Oct 17 '15 at 18:20
  • Thank you for your comment, I have seen this article. **how many subviews are there and can you tell what type they are?** I cannot debug or print subview counts. I don't know why! the print function works correctly in ViewController of application but not working in added keyboard file: UIInputViewController! – Mansour Oct 18 '15 at 12:56

0 Answers0