nil object in iOS8 delegate methods - custom keyboards

后端 未结 1 1871
逝去的感伤
逝去的感伤 2021-02-01 02:05

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

相关标签:
1条回答
  • 2021-02-01 02:34

    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.

    0 讨论(0)
提交回复
热议问题