Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

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

question from:https://stackoverflow.com/questions/24596272/nil-object-in-ios8-delegate-methods-custom-keyboards

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...