shouldChangeCharactersInRange调用两次 [英] shouldChangeCharactersInRange call twice

查看:626
本文介绍了shouldChangeCharactersInRange调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Sometimes, shouldChangeCharactersInRange is called twice when I hit only one character.

作为一个例子,我输入'avion par c',程序被调用并加我第二个'c'结果是'avion par cc' - 我怎么能避免这种情况?

As an exemple, i enter 'avion par c', the procedure is called and add me a second 'c' result is 'avion par cc' - How can I avoid that ?

这只发生在我将应用程序迁移到iOS 7,xCode 5之后。 / p>

This happened only since I migrate the application to 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;
}


推荐答案

此例程在字符被添加到字符串中,只是为了检查是否允许字符等。

This routine is called before the characters are added to the string, just to check if the characters are allowed etc.

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

You are manually adding the characters to the textfield. That is not necessary, you get that functionality delegated. The only thing you have to do in you code is to return YES or NO.

这篇关于shouldChangeCharactersInRange调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆