UITextField How to check if a delete key is pressed

自作多情 提交于 2019-12-03 14:25:01

You can get the string that is supposed to be in text field after this method:

NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

And that neString you probably can use 'as is' for searching the database.

If you just want to get the event when user deletes some characters in textField - then you can check it the following way:

if ([string length] == 0 && range.length > 0)
  //Some characters deleted
ayalcinkaya

for swift users:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    if string.characters.count == 0 && range.length > 0 {
        // Back pressed
        return false
    }

    return true
}

Better idea - in textField:shouldChangeCharactersInRange:replacementString: set up a conditional to return NO when there are no more characters...

if ((range.location == 0) && (string.length == 0))
{        
    NSLog(@"is cleared!");
    return NO;
}

return YES;
user3044484
if (self.textView.text.length > 0) 
{ 
    [self.textView deleteBackward]; 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!