UITextInputDelegate方法无法正常运行

Tac*_*Lee 13 ios8 ios-app-extension custom-keyboard

我现在正在使用iOS 8自定义键盘扩展,使用UITextInputDelegate方法时存在一些问题.

这是否正确:selectionWillChange:selectionDidChange:当用户长按输入区域时应该调用方法?并且textWillChange:textDidChange:方法应该在文本字面改变时调用?

实际上,我观察到的是,当我在文本输入区域中更改选择时,会调用textWillChange:和textDidChange:,并且我无法得到其他两个方法在什么条件下被调用的线索.如果有人知道这些委托方法的用法,请告诉我.

Alb*_*haw 5

它对我也不起作用...我目前正在做的只是使用,textWillChange并且textDidChange正如您所提到的,当您更改选择时,它会被调用...(它们在之前之后被调用)

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

之前(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!");
    }   
}
Run Code Online (Sandbox Code Playgroud)