首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加SearchBar方法

添加SearchBar方法
EN

Stack Overflow用户
提问于 2012-02-01 23:48:42
回答 4查看 464关注 0票数 0

当我们将一些文本添加到uisearchbar中时,我们会在它的右侧看到一个十字。当我们单击此按钮时,uisearchbar中的文本将被清除。这是一个显示十字架的image

我需要做的是,我需要知道事件将如何响应这个十字(当单击时)。

当用户单击这个十字时,我需要打印一个NSLog(@"Cross clicked");

我如何通过编程来做到这一点呢?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-02-01 23:57:11

尝试查看UISearchBarDelegate文档:

代码语言:javascript
复制
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText // called when text changes (including clear)
票数 0
EN

Stack Overflow用户

发布于 2012-02-01 23:58:03

在你的UIViewController中使用UISearchbarDelegate-Protocol。有一个方法将被调用。

代码语言:javascript
复制
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UISearchBarDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UISearchBarDelegate

票数 0
EN

Stack Overflow用户

发布于 2012-02-02 00:52:49

如果您想使用clear按钮执行不同的操作..您可以构建一个自定义事件,而不是默认的事件,并在单击时实现任何事件。

代码语言:javascript
复制
    UISearchBar *searchBar = yourSearchBar;
UITextField *searchtextfield = [searchBar.subviews objectAtIndex:1];
UIButton *cButton = [UIButton buttonWithType:UIButtonTypeCustom];
cButton.frame = CGRectMake(0, 0, 20 , 20);
cButton.backgroundColor = [UIColor clearColor];
[cButton setImage:[UIImage newImageFromResource:@"yourButtonImage"] forState:UIControlStateNormal];//If you want like the x button put image similar to it.
cButton.contentMode = UIViewContentModeScaleToFill;
[cButton addTarget:self action:@selector(clearButtonPressed) forControlEvents:UIControlEventTouchUpInside];//This is the custom event
[searchtextfield setRightView:cButton];
[searchtextfield setRightViewMode:UITextFieldViewModeAlways];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9098816

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档