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?
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 sendtextWillChange:
,textDidChange:
,selectionWillChange:
, andselectionDidChange:
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 sendselectionWillChange:
before you change the selected range, and you sendselectionDidChange:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With