1

我是 iOS 编程新手。我正在为它的视图开发一个带有自定义 nib 文件的自定义键盘。如何在 nib 文件中为按钮添加目标而不手动将它们连接到我的 UIInputViewController?我试过这段代码,但不起作用!

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!)
}

}

4

0 回答 0