我有一个习惯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)
}
}