13

I'm working on iOS 8 custom keyboard extension right now, and there are some issues in using UITextInputDelegate Methods.

Does this right: selectionWillChange: and selectionDidChange: methods should be called when user long-presses typing area? And textWillChange: and textDidChange: methods should be called whenever the text is literally changing?

Actually, what I observed is that, when I changed selection in text input area, textWillChange: and textDidChange: are called, and I cannot get a clue that the other two methods are called in what condition. If anyone knows about the usage of these delegate methods, please let me know.

4

2 回答 2

3

它也对我不起作用......我目前正在做的只是使用textWillChange并且textDidChange确实被调用,正如你所提到的,当你改变你的选择时......(他们被称为BEFOREAFTER

然后比较:
self.textDocumentProxy.documentContextBeforeInput
self.textDocumentProxy.documentContextAfterInput

From BEFORE (textWillChange) 到AFTER (textDidChange) 以查看选择范围或长度是否发生了变化。


像这样的东西(当然在你的 .h 文件中设置下面的 4 个 NSStrings ......还没有测试过这个确切的片段,因为我刚刚在 SO.com 上从头开始编写它,但我确信如果我做了任何这个原则是有效的错误)

- (void)textWillChange:(id<UITextInput>)textInput {
    beforeStringOfTextBehindCursor = self.textDocumentProxy.documentContextBeforeInput;
    beforeStringOfTextAfterCursor = self.textDocumentProxy.documentContextAfterInput;
}

- (void)textDidChange:(id<UITextInput>)textInput {
    afterStringOfTextBehindCursor = self.textDocumentProxy.documentContextBeforeInput;
    afterStringOfTextAfterCursor = self.textDocumentProxy.documentContextAfterInput;

    BOOL didSelectionChange = NO;

    if (![beforeStringOfTextBehindCursor isEqualToString:afterStringOfTextBehindCursor]) {
        didSelectionChange = YES;
    }

    if (![beforeStringOfTextAfterCursor isEqualToString:afterStringOfTextAfterCursor]) {
        didSelectionChange = YES;
    }

    if (didSelectionChange) {
        NSLog(@"Selection Changed!");
    }   
}
于 2014-08-10T04:02:25.930 回答
-1

我在 UITextInput 协议中指定的函数没有被调用时遇到了同样的问题。据我所知,原因是 inputDelegate 是在运行时设置的。根据 ios 文档:

UIKit 提供了一个私有文本输入委托,它在运行时将其分配给类采用 UITextInput 协议的对象的 inputDelegate 属性。 关联

在我的情况下有效的修复是重置函数中的 inputDelegate:

textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
     replacementString:(NSString *)string

通过以下行:

[myUITextField setInputDelegate:self];

其中self实现了 UITextInputDelegate 协议。

于 2014-11-25T13:35:49.253 回答