0

I'm developing a custom keyboard for iOS, using the UIInputViewController I have implemented the following methods: textWillChange, textDidChange.

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. 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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Emil Adz
  • 40,709
  • 36
  • 140
  • 187

1 Answers1

0

I am using Objective C, as I have this code available at this point of time:-

You can use KVO , I guess for your objective:-

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]);
          }
    }
Vizllx
  • 9,135
  • 1
  • 41
  • 79
  • Generally, UIKit is not KVO-compliant. http://stackoverflow.com/questions/6333600/does-key-value-observing-work-on-uitextviews-text-property – Anton Jun 03 '15 at 10:14
  • @Anton Thanks for commenting! I think you are missing the concept here! Let me clarify you, " KVO won’t necessarily work for the keys that already exist in that UIKit class" so I am using a different approach here. So before commenting go for a in-depth research on it. By the way, KVO is working fine in all of my apps which are currently in app store and even updated to iOS 8. – Vizllx Jun 03 '15 at 10:19
  • @Vizllx I'm not saying no one should use KVO. UIKit's KVO compliance is accidental and you should not rely on it, as one of the UIKit developers said (Dave DeLong). Please see his answer here: http://stackoverflow.com/a/6738557/430633. For this reason ReactiveCocoa developers added `rac_textSignal` to UITextView and UITextField components - just because "text" property is not "strictly" KVO compliant, KVO-ing on them can lead to strange bugs. – Anton Jun 03 '15 at 10:54
  • @Anton Seems like you took time to do in-depth research . Anyways,just read this then - https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/Compliant.html#//apple_ref/doc/uid/20002172-BAJEAIEE .The answer Dave given will never be considered true until and unless it is documented. Moreover I have not got any crash issues till now, neither Crashlytics has reported me of any such crashes reported for using KVO. – Vizllx Jun 03 '15 at 11:15