UITextInputDelegate methods not functioning correctly

帅比萌擦擦* 提交于 2019-11-29 12:41:57

问题


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.


回答1:


It's not working for me either... what I am currently doing is just using textWillChange and textDidChange which does get called, as you mentioned, when you change your selection... (they get called BEFORE and AFTER)

And then comparing the:
self.textDocumentProxy.documentContextBeforeInput
self.textDocumentProxy.documentContextAfterInput

From BEFORE (textWillChange) to the AFTER (textDidChange) to see if selection range or length changed at all.


Something like this (set the 4 NSStrings below in your .h file of course... haven't tested this exact snippet because I wrote it from scratch just now on SO.com but I'm sure the principle works if I made any errors)

- (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!");
    }   
}



回答2:


I had the same problem with the functions specified in the UITextInput protocol not being called. The reason as far as I can discern is due to the fact that the inputDelegate is set at runtime. According to the ios docs:

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. link

The fix which works in my case is to reset the inputDelegate in the function:

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

by the following line:

[myUITextField setInputDelegate:self];

where self implements the UITextInputDelegate protocol.



来源:https://stackoverflow.com/questions/25158161/uitextinputdelegate-methods-not-functioning-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!