Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nil object in iOS8 delegate methods - custom keyboards

I'm building a custom keyboard and I'm implementing the following delegate methods in my InputViewController.
But I always get _textInput = nil_

- (void)textWillChange:(id<UITextInput>)textInput
- (void)textDidChange:(id<UITextInput>)textInput
- (void) selectionWillChange:(id<UITextInput>)textInput
- (void) selectionDidChange:(id<UITextInput>)textInput

Does anybody know how to fix it?
Is it nil for a reason?
Do I need to implement something by myself?

like image 837
nurnachman Avatar asked Jul 06 '14 13:07

nurnachman


1 Answers

Good question. But it seems that UITextInputDelegate is not a protocol that you implement.

From Apple Docs titled Lower Level Text-Handling Technologies:

When changes occur in the text view due to external reasons—that is, they aren't caused by calls from the text input system—the UITextInput object should send textWillChange:, textDidChange:, selectionWillChange:, and selectionDidChange: messages to the input delegate (which it holds a reference to). For example, when users tap a text view and you set the range of selected text to place the insertion point under the finger, you would send selectionWillChange: before you change the selected range, and you send selectionDidChange: after you change the range.

And from the docs on UITextInputDelegate:

The UIKit provides a private text input delegate, which it assigns at runtime to the inputDelegate property of the object whose class adopts the UITextInput protocol.

The implication of the above is that we don't implement these delegate methods; we use them to inform the inputDelegate that you have changed your text or selection via means other than keyboard input.

Here is an example method that illustrates this:

- (void)delete:(id)sender;
{
    if (selection && ![selection isEmpty]) {
        [inputDelegate textWillChange:self];
        [inputDelegate selectionWillChange:self];
        [self replaceRange:selection withText:@""];
        [inputDelegate selectionDidChange:self];
        [inputDelegate textDidChange:self];
    }
}

Sample code with more examples here.

like image 124
Drakes Avatar answered Oct 07 '22 07:10

Drakes