【问题标题】:shouldChangeCharactersInRange call twiceshouldChangeCharactersInRange 调用两次
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      相关资源
      最近更新 更多