问题描述
我正在构建一个自定义键盘
,我正在我的 InputViewController
中实现以下委托方法。
但我总是获取 _textInput = nil _
I'm building a custom keyboardand 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
有人知道如何解决它吗?
是 nil
为什么?
我需要自己实现吗?
Does anybody know how to fix it?
Is it nil
for a reason?
Do I need to implement something by myself?
推荐答案
好问题。但是似乎 UITextInputDelegate
不是您实现的协议。
Good question. But it seems that UITextInputDelegate
is not a protocol that you implement.
从苹果标题为:
从:
And from the docs on UITextInputDelegate:
上面的含义是我们不实现这些委托方法;我们使用它们通知 inputDelegate
,您已经通过除键盘输入之外的方式更改了您的文本或选择。
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.
这篇关于在iOS8委托方法中的零对象 - 自定义键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!