简体   繁体   English

如何测试用户是否更改了UITextInput?

[英]How to test if User has changed UITextInput?

I'm developing a custom keyboard for iOS, using the UIInputViewController I have implemented the following methods: textWillChange , textDidChange . 我正在使用UIInputViewController开发用于iOS的自定义键盘,我实现了以下方法: textWillChangetextDidChange

I want to know when user changes the current text field and moves to another one in order to configure the Auto Capitalization parameters of the currently pressed text field in the keyboard. 我想知道用户何时更改当前文本字段并移至另一个文本字段,以便在键盘上配置当前按下的文本字段的自动大写参数。

Now I tried to do something like this: 现在我试图做这样的事情:

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
}

Or even something like this: 甚至是这样的:

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

But none of those options works for me. 但是这些选择都不适合我。

UPDATE: As you can notice the object I receive in the callback methods is not a UITextField object but a UITextInput object which is actually a protocol, and not the text field itself. 更新:正如您所注意到的,我在回调方法中收到的对象不是UITextField对象,而是实际上是协议的UITextInput对象,而不是文本字段本身。 So I can't add observer to it. 所以我不能添加观察者。 My keyboard application does not control the fields the user enters text into. 我的键盘应用程序无法控制用户输入文本的字段。

Question: how can I recognize the moment that the user changes his current text field? 问题:如何识别用户更改其当前文本字段的时间?

I am using Objective C, as I have this code available at this point of time:- 我正在使用Objective C,因为此时此代码可用:

You can use KVO , I guess for your objective:- 您可以使用KVO ,我想您的目标是:

static void *contextNew = &contextNew;

//Observe change to txtfeild.text:

[self.txtfeild addObserver:self forKeyPath:@"text"
            options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld 
            context:contextNew];


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
        change:(NSDictionary *)change context:(void *)context {

          if(context == contextNew) {

              NSLog(@"txtfeild new text : %@ - txtfeild Old text: %@",
              [change objectForKey:NSKeyValueChangeNewKey],
              [change objectForKey:NSKeyValueChangeOldKey]);
          }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM