次のメソッドを実装した UIInputViewController を使用して、iOS 用のカスタム キーボードを開発しています: textWillChange、textDidChange。
キーボードで現在押されているテキスト フィールドの自動大文字化パラメータを設定するために、ユーザーが現在のテキスト フィールドを変更して別のフィールドに移動したときを知りたいです。
今、私はこのようなことをしようとしました:
override func textWillChange(textInput: UITextInput) {
if !textInput.isEqual(self.mCurrentTextInput) {
Logger.printLogToConsole(TAG, aMethodName: __FUNCTION__, aMessage: "TextInput has changed!")
KeyboardState.sharedInstance.setAutoCapitalizationFlag()
}
self.mCurrentTextInput = textInput
}
または、次のようなものでもあります。
override func textWillChange(textInput: UITextInput) {
if self.mCurrentTextInput == nil {
if let view = textInput.textInputView {
self.mCurrentTextInput = view
KeyboardState.sharedInstance.setAutoCapitalizationFlag()
}
Logger.printLogToConsole(TAG, aMethodName: __FUNCTION__, aMessage: "TextInput has changed!")
} else if self.mCurrentTextInput!.isEqual(textInput) {
Logger.printLogToConsole(TAG, aMethodName: __FUNCTION__, aMessage: "TextInput has changed!")
if let view = textInput.textInputView {
KeyboardState.sharedInstance.setAutoCapitalizationFlag()
self.mCurrentTextInput = view
}
}
}
しかし、これらのオプションはどれも私にはうまくいきません。
更新:お気づきのとおり、コールバック メソッドで受け取るオブジェクトは UITextField オブジェクトではなく、実際にはプロトコルである UITextInput オブジェクトであり、テキスト フィールド自体ではありません。したがって、オブザーバーを追加することはできません。私のキーボード アプリケーションは、ユーザーがテキストを入力するフィールドを制御しません。
質問:ユーザーが現在のテキスト フィールドを変更した瞬間をどのように認識できますか?