在iOS8委托方法中的零对象 - 自定义键盘 [英] nil object in iOS8 delegate methods - custom keyboards
问题描述
我正在构建一个自定义键盘
,我正在我的 InputViewController
中实现以下委托方法。
但我总是获取 _textInput = nil _
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
有人知道如何解决它吗?
是 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.
从苹果标题为较低级文本处理的文档技术:
由于外部原因,文本视图中发生更改 - 即
他们不是' t由文本输入系统的调用引起的 -UITextInput
对象应发送textWillChange:
,textDidChange:
,
selectionWillChange:
和selectionDidChange:
消息到输入
委托(它持有引用)。例如,当用户点击
a文本视图,并设置所选文本的范围以将
插入点放在手指下方时,您将发送selectionWillChange:
更改所选范围之前,您更改范围后发送selectionDidChange:
。
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.
从 docs :
And from the docs on UITextInputDelegate:
UIKit提供了一个私有文本输入代理,它以
运行时分配给其类采用
的UITextInput协议的对象的inputDelegate属性。
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.
上面的含义是我们不实现这些委托方法;我们使用它们通知 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];
}
}
具有更多示例的示例代码 here 。
Sample code with more examples here.
这篇关于在iOS8委托方法中的零对象 - 自定义键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!