UITextField如何检查是否按下了删除键 [英] UITextField How to check if a delete key is pressed

查看:179
本文介绍了UITextField如何检查是否按下了删除键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从我的本地数据库实现一个搜索栏,当用户输入信息时从db搜索。问题是我将最近的字符和之前的字符连接起来然后发送给搜索。如何删除字符(最后一个)当按下后退键时。我正在使用

I am implementing a search bar from my local database that searches from db as user enters info.The issue is that i concat recent character and the previous ones and then send it for search.How can I REMOVE the character (last one) when back key is pressed.I am using


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

感谢您的回复

推荐答案

您可以在此方法后获取应该位于文本字段中的字符串:

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

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

那个 neString 你可能会用'用于搜索数据库。

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

如果您只是想在用户删除textField中的某些字符时获取该事件 - 那么您可以通过以下方式进行检查:

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

这篇关于UITextField如何检查是否按下了删除键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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