1

我有一个习惯UIInputViewController,就叫它吧MyInputViewController。我将文本字段的输入视图设置为输入控制器的单例实例的视图。插入、删除和移动光标工作正常,但是当我调用self.dismissKeyboard()输入视图控制器(调用方法,print() 写入控制台)时,它什么也不做。

有什么问题?它在几天前开始工作,但我不记得可能是什么错误。

class MyInputController: UIInputViewController {

// SINGLETON:
static let keyboard: MyInputController = MyInputController()

class func setSharedKeyboardForTextField(textField: UITextField) {
    textField.inputAccessoryView = nil
    textField.inputView = keyboard.view
}

// MARK: initializers
private init() {
    super.init(nibName: nil, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func loadView() {
    super.loadView()
    self.view.translatesAutoresizingMaskIntoConstraints = false=
}

override func viewDidLoad() {
    //set up buttons
}

// MARK: helper functions
func inputText() -> String {
    return (self.textDocumentProxy.documentContextBeforeInput ?? "") + (self.textDocumentProxy.documentContextAfterInput ?? "")
}

// MARK: animations
private func grow(sender: MathematicalKeyboardKey) {
    sender.superview?.bringSubviewToFront(sender)
    UIView.animateWithDuration(0.1) { () -> Void in
        sender.transform = scaleTransform
    }
}

private func shrink(sender: MathematicalKeyboardKey) {
    sender.superview?.sendSubviewToBack(sender)
    UIView.animateWithDuration(0.1) { () -> Void in
        sender.transform = CGAffineTransformIdentity
    }
}

// MARK: actions
func keyTouched(sender: MathematicalKeyboardKey) {
    // works
    grow(sender)
}

func keyExit(sender: MathematicalKeyboardKey) {
    // works
    shrink(sender)
}

func resign(sender: MathematicalKeyboardKey) {
    // called but doesn't work
    dismissKeyboard()
    shrink(sender)
}

func keyPressed(sender: MathematicalKeyboardKey) {
    // works
    self.textDocumentProxy.insertText(sender.insertion)
    shrink(sender)
}

func remove(sender: MathematicalKeyboardKey) {
    // works
    self.textDocumentProxy.deleteBackward()
    shrink(sender)
}

func clear(sender: MathematicalKeyboardKey) {
    // works
    while self.textDocumentProxy.hasText() {
        self.textDocumentProxy.deleteBackward()
    }
    shrink(sender)
}

func moveLeft(sender: MathematicalKeyboardKey) {
    // works
    self.textDocumentProxy.adjustTextPositionByCharacterOffset(-1)
    shrink(sender)
}

func moveRight(sender: MathematicalKeyboardKey) {
    // works
    self.textDocumentProxy.adjustTextPositionByCharacterOffset(1)
    shrink(sender)
}

}

4

1 回答 1

-1

您可以使用 ;

 resignFirstResponder()

方法代替

 dismissKeyboard()

方法。

@IBAction func hideKeyboard(sender: UIButton) {

    resignFirstResponder()

}
于 2016-01-11T13:34:49.153 回答