-1

我有一个UItextfield基于预定义字符串的自动完成其值。如果第一个字符串中有多个单词currentString,则结果显示将currentString附加最后一个字符。

所以在示例代码中

键入No时,UITextFields 结果文本变为Note todayt(附加到字符串的第二个单词的最后一个字母)

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

NSString *currentString = [_txt.text stringByReplacingCharactersInRange:range withString:string];


 NSString *currentString = [_txt.text stringByReplacingCharactersInRange:range withString:string];
if ([currentString isEqualToString:@"No t"]) {
    textField.text = @"Note today";

有没有办法可以避免这种行为?

我努力了

 if(_txt.text.length>1)
    {
       _txt.text =[textField.text substringToIndex:[textField.text length] - 1];

    }

if(_txt.text.length>1)
    {
        NSString *text2;

        NSString *currentString = textField.text;
        NSString *newString = [text2 substringToIndex:[text2 length]-1];
        textField.text=newString;
    }

    return NO;
}
return YES;

}
4

2 回答 2

2

我敢打赌你会返回 YES,这会导致输入“t”。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range  replacementString:(NSString *)string {
  NSString *currentString = [_txt.text stringByReplacingCharactersInRange:range withString:string];

  if ([currentString isEqualToString:@"No t"]) {
    textField.text = @"Note today";
    return NO;
  }

  return YES;
}
于 2013-01-19T01:37:32.117 回答
0

好的排序这个,我添加了一个 if 语句来删除多余的字符

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

NSString *currentString = [_txt.text stringByReplacingCharactersInRange:range  withString:string];


NSString *currentString = [_txt.text stringByReplacingCharactersInRange:range withString:string];
if ([currentString isEqualToString:@"No t"]) {
textField.text = @"Note today";

  }


 if(_txt.text.length>=4)

  {
        NSString *text;

        text =[[textField text] stringByAppendingString:string];
        NSString *newString = [text substringToIndex:[text length]-1];

        textField.text=newString;


    return NO;

   }
   return YES;
}
于 2013-01-19T10:57:18.567 回答