When the searchBar:textDidChange: method of the UISearchBarDelegate gets called because of the user tapping the 'clear' button, the searchBar hasn't become the first responder yet, so we can take advantage of that in order to detect when the user in fact intended to clear the search and not bring focus to the searchBar and/or do something else.

To keep track of that, we need to declare a BOOL ivar in our viewController that is also the searchBar delegate (let's call it shouldBeginEditing) and set it with an initial value of YES (supposing our viewController class is called SearchViewController):

@interface SearchViewController : UIViewController <UISearchBarDelegate> {
// all of our ivar declarations go here...
BOOL shouldBeginEditing;
....
} ...
@end @implementation SearchViewController
...
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
...
shouldBeginEditing = YES;
}
}
...
@end

Later on, in the UISearchBarDelegate, we implement the searchBar:textDidChange: andsearchBarShouldBeginEditing: methods:

- (void)searchBar:(UISearchBar *)bar textDidChange:(NSString *)searchText {
NSLog(@"searchBar:textDidChange: isFirstResponder: %i", [self.searchBar isFirstResponder]);
if(![searchBar isFirstResponder]) {
// user tapped the 'clear' button
shouldBeginEditing = NO;
// do whatever I want to happen when the user clears the search...
}
} - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)bar {
// reset the shouldBeginEditing BOOL ivar to YES, but first take its value and use it to return it from the method call
BOOL boolToReturn = shouldBeginEditing;
shouldBeginEditing = YES;
return boolToReturn;
}

http://stackoverflow.com/questions/1092246/uisearchbar-clearbutton-forces-the-keyboard-to-appear/3852509#3852509

UISearchBar clearButton的更多相关文章

  1. iOS之让UISearchBar搜索图标和placeholder靠左显示

    系统UISearchBar效果图: 需求效果图: 两种方案: 找到UISearchBar上的放大镜图标, 修改Frame. 同时判断在有无文本内容更改placeholder的颜色. 利用UISearc ...

  2. iOS 如何自定义UISearchBar 中textField的高度

    iOS 如何自定义UISearchBar 中textField的高度 只需设置下边的方法就可以 [_searchBar setSearchFieldBackgroundImage:[UIImage i ...

  3. iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  4. 更改UIsearchbar 的背景和cancel按钮(转)

    修改背景 searchbar =[[UISearchBar alloc]initWithFrame:CGRectMake(,KTopBarHeight, , KTopBarHeight)]; sear ...

  5. UISearchBar控件-让我们来搞定!(转)

    转载自:http://blog.sina.com.cn/s/blog_7b9d64af0101dfg8.html     最近用到搜索功能.于是,经过不断的研究,终于,有点懂了. 那就来总结一下吧,好 ...

  6. iOS开发——UI进阶篇(十九)UISearchBar控件简介

    最近用到搜索功能.总结一下 搜索,无疑可以使用UISearchBar控件! 那就先了解一下UISearchBar控件吧! UISearchBar控件就是要为你完成搜索功能的一个专用控件.它集成了很多你 ...

  7. IOS开发UISearchBar失去第一响应者身份后,取消按钮不执行点击事件的问题

    在iOS开发中,使用UISearchBar的时候,当搜索框失去焦点的时候,取消按钮是默认不能点击的,如图按钮的颜色是灰色的:  这是因为此时取消按钮的enabled属性被设置为NO了,那么当我们需要让 ...

  8. 修改UISearchBar的背景颜色

    当你看到这篇博客你就已经发现了用_searchBar.backgroundColor = [UIColor clearColor];来设置UISearchBar的颜色完全没有效果: 并且,有些方法是想 ...

  9. iOS UISearchBar 设置取消按钮,回收键盘,并修改cancel为“取消”

    继承协议: UISearchBarDelegate 在代理方法中设置: #pragma mark --- 搜索框开始编辑 --- - (void)searchBarTextDidBeginEditin ...

随机推荐

  1. sort()的升降序函数操作

    sort()函数包含在头文件#include <algorithm>中,是常用的排序函数. functional提供了一堆基于模板的比较函数对象.它们是:equal_to<Type& ...

  2. C++STL 求和:accumulate 【转】

    该算法在numeric头文件中定义. accumulate()的原型为(文件取自DEV-C++编译器): template<typename _InputIterator, typename _ ...

  3. UVa-10954

    10954 - Add All Time limit: 3.000 seconds Yup!! The problem name reflects your task; just add a set ...

  4. window.showModalDialog 在谷歌Uncaught TypeError: undefined is not a function

    if(navigator.userAgent.indexOf("Chrome") >0 ){var winOption = "height="+heigh ...

  5. 《剑指offer》面试题15—输出链表中倒数第n个结点

    题目:如题,且从1开始计数. 思路:要求只遍历一遍链表:设置两个指针,一个先走n步后另一个开始同步后移,当快指针已经到链表尾时慢指针正好到要输出的结点. 注意:本题思路比较好想到,主要考察的是代码的鲁 ...

  6. 模板 - 洲阁筛 + min25筛

    好像在某些情况下杜教筛会遇到瓶颈,先看着.暑假学一些和队友交错的知识的同时开这个大坑.

  7. HDU5113【DFS+剪枝】

    题意: n*m的矩阵 k种颜色 每种颜色有c[i]个 上下左右相邻的格子不能一样的颜色 问你有没有一种染色方法,有的话输出方案. 思路: 暴搜啊,n,m都才5,做完以后大哥的剪枝是奇偶剪枝,其实画完图 ...

  8. 异步编程(AsyncCallback委托,IAsyncResult接口,BeginInvoke方法,EndInvoke方法的使用小总结)

    http://www.cnblogs.com/panjun-Donet/archive/2009/03/03/1284700.html 让我们来看看同步异步的区别: 同步方法调用在程序继续执行之前需要 ...

  9. JS与JQ的对比与提高

    来吧, 案例1:先上个例子js写的省市二级联动 <!DOCTYPE html><html> <head> <meta charset="UTF-8& ...

  10. C++中各种简写及全称的库

    ATL(Active TEmplate Library)活动模板库 RPC(Remote Procedure Call Protocol)远程过程调用协议 DCE(Distributed Comput ...