gpt4 book ai didi

ios - shouldChangeCharactersInRange 调用两次

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:49 40 4
gpt4 key购买 nike

有时,当我只点击一个字符时,shouldChangeCharactersInRange 会被调用两次。

例如,我输入“avion par c”,程序被调用并添加我第二个“c”结果是“avion par cc”——我怎样才能避免这种情况?

这只是在我将应用程序迁移到 iOS 7、xCode 5 之后才发生的。

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

wordSearching = YES;

if ([string isEqualToString:@"\n"]) {
[self textFieldDoneEditing];
return NO;
}

if ([string isFirstCharacterPunctuation]) {
NSLog(@"punctuation detected");
[self replaceWordEditedByFirstSuggestion];
[self hideSuggestions];
return YES;
}

NSLog(@"Search : %@", string);
//NSLog(@"textField : %@", textField);
NSLog(@"self : %@", self.textEdited);


string = [string lowercaseString];

self.textEdited = [[NSString stringWithString:textField.text] lowercaseString];
self.textEdited = [textEdited stringByReplacingCharactersInRange:range withString:string];
indexEdition = MIN(range.location + 1, [textEdited length]);
self.wordEdited = [textEdited wordAtIndex:indexEdition];

NSLog(@"editing at [%d '%@' '%@' '%@']", indexEdition, wordEdited, string, textEdited);

if ([self canBeDirectAccess:textField.text]) {
//[self hideSuggestions];
NSLog(@"Direct Access");
return YES;
}

[self updateSuggestionsFor:wordEdited];

return YES;
}

最佳答案

这个例程在字符被添加到字符串之前被调用,只是为了检查字符是否被允许等。

您正在手动将字符添加到文本字段。那不是必需的,您可以委派该功能。您在代码中唯一需要做的就是返回 YES 或 NO。

关于ios - shouldChangeCharactersInRange 调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20353191/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com