iphone - 如何在输入时获取搜索栏的当前字符串

标签 iphone objective-c xcode ios5

在按下搜索栏时,我想获取已经输入的字符串。为此,我目前正在使用这种方法:

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range     replacementText:(NSString *)text
{
    NSLog(@"String:%@",mainSearchBar.text);
    return YES;
}

但它返回的是之前的字符串。例如 id 我输入“jumbo”,它显示 jumb,当我按退格键删除一个项目并使其成为“jumb”时,它显示 jumbo。即搜索栏上的前一个字符串。

如何获取当前字符串?请帮助。 谢谢

最佳答案

在获取输入文本的方法中:

NSString* newText = [searchBar.text stringByReplacingCharactersInRange:range withString:text]

swift 3:

let newText = (searchBar.text ?? "" as NSString).replacingCharacters(in: range, with: text)

关于iphone - 如何在输入时获取搜索栏的当前字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10161833/

相关文章:

ios - 如何在 Google Analytics 仪表板上看到 'User timing'?

iphone - Xcode iPhone编程: Loading a jpg into a UIImageView from URL

iphone - 如何使用 json 将更新的数据发送到 iphone 中的服务器?

objective-c - 如何为不同的build设置目标设置

objective-c - Xcode for How to call function from one class to another class function in iPhone Apps

iphone - 使后台工作在所有 iphone ios 上

iphone - MyObject对象是否可能;包含垃圾?

Windows 错误的 iPhone 配置实用程序

iphone - Star Micronics TSP 650 打印机和 iOS SDK

ios - "No rows found"当没有 UICollectionView 和 UITableView 的行/单元格时