删除在UItextfield中键入的最后一个字符

时间:2012-03-23 11:42:25

标签: iphone objective-c ios ipad uitextfield

如果文本字段长度超过100,我需要删除最后一个字符,我使用了以下代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textfield.text.length>=100)
    {
        NSString *text;
        textField.text=text;
        NSString *newString = [text substringToIndex:[text length]-1];
        textField.text=newString;
    }
    return YES;
}

但它只是删除了整个文本。

7 个答案:

答案 0 :(得分:5)

由于这一行,您删除了整个文本:

textField.text=text; // text is nil here

您想要做的更有可能是:

NSString *text = textField.text;

答案 1 :(得分:3)

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

      if(textfield.text.length>=100)
     {
         NSString *text;
         text =[[textField text] stringByAppendingString:string];
         NSString *newString = [text substringToIndex:[text length]-1];

         textField.text=newString;
     }
     return YES;

 }

粘贴代码可能会解决您的问题

答案 2 :(得分:3)

在iOS 5或更高版本中:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([textField.text length] >= 100)
    {
        [textField deleteBackward];
    }
}

答案 3 :(得分:2)

尝试使用以下代码

 - (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText {

    NSString* newText = [textView.text stringByReplacingCharactersInRange:aRange withString:aText];

    // CGSize strSize = [newText sizeWithFont:textView.font constrainedToSize:CGSizeMake(200, 10000) lineBreakMode:UILineBreakModeWordWrap];

   if([newText length] > 100)
   {
       return NO; // can't enter more text
   }
   else
      return YES; // let the textView know that it should handle the inserted text
}

答案 4 :(得分:0)

试试这个:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
return !([newString length] >100);
}

答案 5 :(得分:0)

为了确定locals[2]长度,您不必使用字符串连接。只需使用这个简单的代码:

UITextField

这样您就像-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ return !([string length]+[textField.text length] > FIXED_LENGTH); } 一样说:"将UITextFieldDelegate追加UITextField更改为string 如果所有字符的总和不超过最大长度"

答案 6 :(得分:0)

以下是如何删除UITextField中的任何文本的示例;适用于iOS 10.3及更高版本

@IBOutlet var upcText: UITextField!
if var textRange = upcText.selectedTextRange {
    if textRange.isEmpty && textRange.start != upcText.beginningOfDocument {
        // text has not been selected and the cursor is not at the beginnning
        // then create new range to delete previous character
        textRange = upcText.textRange(from: upcText.position(from: textRange.start, offset: -1)!, to: textRange.start)!
    }
    upcText.replace(textRange, withText: "")
}