Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSString隐藏键盘时字符串的范围问题_Iphone_Ios_Keyboard_Nsstring_Uitextfield - Fatal编程技术网

Iphone NSString隐藏键盘时字符串的范围问题

Iphone NSString隐藏键盘时字符串的范围问题,iphone,ios,keyboard,nsstring,uitextfield,Iphone,Ios,Keyboard,Nsstring,Uitextfield,我正在实现一个列表,该列表可以在UITextFieldDelegate的帮助下使用UITextField中的文本进行过滤 代码如下: - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { _tempWrittenText = [textField.text stringByReplacin

我正在实现一个列表,该列表可以在
UITextFieldDelegate
的帮助下使用
UITextField
中的文本进行过滤

代码如下:

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

    _tempWrittenText = [textField.text stringByReplacingCharactersInRange: range withString: string];
    [self filterCountries];

    return YES;
}

/** Filter the countries with the currently typed text */
- (void) filterCountries {
    if (_tempWrittenText.length == 0) {
        _visibleCountriesList = (NSMutableArray*) _countriesList;
        [tableMedals reloadSections: [NSIndexSet indexSetWithIndex: 0] withRowAnimation: UITableViewRowAnimationNone];

    } else {
        _visibleCountriesList = [NSMutableArray array];

        for (Country* c in _countriesList) {
            if ([c.name rangeOfString: _tempWrittenText].location != NSNotFound) {
                [_visibleCountriesList addObject: c];
            }
        }
        [tableMedals reloadSections: [NSIndexSet indexSetWithIndex: 0] withRowAnimation: UITableViewRowAnimationFade];
    }

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return YES;
}
当输入文本时,过滤器工作正常;但是,当按下“完成”键盘键(隐藏键盘)时,也会触发
-(BOOL)textField:(UITextField*)textField shouldChangeCharactersRange:(NSRange)range replacementString:(NSString*)string
方法,这会奇怪地擦除所有项目,而不是保持原样

问题是
rangeOfString
部分总是返回NSNotFound。我不明白为什么,如果我记录变量,两个字符串是正确的

例如:
[@“Angola”rangeOfString@“A”]。位置将给出NSNotFound

我重复一遍,只有当键盘被隐藏时才会发生这种情况。有人有主意吗

提前感谢

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
按下“完成”按钮时不会激发,否则,如果您强制调用此函数

使用此选项隐藏键盘,不会触发上述方法

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

我刚刚发现,当按下Done键时,系统会添加一个新行字符。我不知道这是默认行为,也不知道它为什么这样做

防止这种情况的方法是添加以下内容:

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

    _tempWrittenText = [textField.text stringByReplacingCharactersInRange: range withString: string];
    [self filterCountries];

    return YES;
}

/** Filter the countries with the currently typed text */
- (void) filterCountries {
    if (_tempWrittenText.length == 0) {
        _visibleCountriesList = (NSMutableArray*) _countriesList;
        [tableMedals reloadSections: [NSIndexSet indexSetWithIndex: 0] withRowAnimation: UITableViewRowAnimationNone];

    } else {
        _visibleCountriesList = [NSMutableArray array];

        for (Country* c in _countriesList) {
            if ([c.name rangeOfString: _tempWrittenText].location != NSNotFound) {
                [_visibleCountriesList addObject: c];
            }
        }
        [tableMedals reloadSections: [NSIndexSet indexSetWithIndex: 0] withRowAnimation: UITableViewRowAnimationFade];
    }

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return YES;
}

希望这对某人有所帮助。

所以,既然
rangeOfString:
实际上不可能有条件地无法正常工作,现在是时候更仔细地检查参数本身了。测井记录没有告诉我太多,但我重新审查了,这是因为添加的字符是新行(“\n”)。是的,这是奇怪的部分。我刚刚发现,当按下“完成”键时,系统正在添加一个新的行字符,这就是为什么一切都不好。我会选择接受您的答案,但是在我的程序中,我真的不需要调用resignFirstResponder来隐藏键盘,因为我将一个函数链接到了DidEndOnExit。您使用的是UITextView,而不是UITextField,在这种情况下,您只需在replacementString上捕获字符串,如果它是新的lineNo,它实际上是一个UITextField。