iOS 9 UIInputViewController dismissKeyboard

时间:2015-09-17 14:20:11

标签: ios swift ios9 uiinputviewcontroller

我有一个自定义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)
}

}

1 个答案:

答案 0 :(得分:-1)

你可以使用;

rand()

方法而不是

 resignFirstResponder()

方法

 dismissKeyboard()