【发布时间】:2013-12-19 15:08:02
【问题描述】:
有时,当我只击中一个字符时,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;
}
【问题讨论】:
标签: ios methods ios7 uitextfield xcode5