当我们将一些文本添加到uisearchbar中时,我们会在它的右侧看到一个十字。当我们单击此按钮时,uisearchbar中的文本将被清除。这是一个显示十字架的image。
我需要做的是,我需要知道事件将如何响应这个十字(当单击时)。
当用户单击这个十字时,我需要打印一个NSLog(@"Cross clicked");
我如何通过编程来做到这一点呢?
发布于 2012-02-01 23:57:11
尝试查看UISearchBarDelegate文档:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText // called when text changes (including clear)发布于 2012-02-01 23:58:03
在你的UIViewController中使用UISearchbarDelegate-Protocol。有一个方法将被调用。
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchTexthttp://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UISearchBarDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UISearchBarDelegate
发布于 2012-02-02 00:52:49
如果您想使用clear按钮执行不同的操作..您可以构建一个自定义事件,而不是默认的事件,并在单击时实现任何事件。
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];https://stackoverflow.com/questions/9098816
复制相似问题