[英]UISearchBar inside cancel button
I am developing an app on XCode 5 and I don't found the answer for this. 我正在XCode 5上开发应用程序,但没有找到答案。
Which method is the one that controls that "X" button on the UISearchBar
? 哪个方法可以控制
UISearchBar
上的“ X”按钮? I tried with 我尝试过
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
But it doesn't work 但这不起作用
Any clue? 有什么线索吗?
Thanks in advance! 提前致谢!
This is the method you are looking for 这是您要寻找的方法
- (BOOL)textFieldShouldClear:(UITextField *)textField;
But this is UITextFields delegate method and you need to set delegate of UISearchBar's textfield. 但这是UITextFields的委托方法,您需要设置UISearchBar的文本字段的委托。 See this
看到这个
In your header (.h) file 在您的标头(.h)文件中
conform to delegates. 符合代表。
<UISearchBarDelegate, UITextFieldDelegate>
- (void)viewDidLoad
{
//find the UITextField view within searchBar (outlet to UISearchBar)
//and assign self as delegate
for (UIView *view in searchBar.subviews)
{
if ([view isKindOfClass: [UITextField class]])
{
UITextField *tf = (UITextField *)view;
tf.delegate = self;
break;
}
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) aSearchBar
{
[aSearchBar resignFirstResponder];
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
[self performSelector:@selector(searchBarCancelButtonClicked:) withObject:self.searchBar afterDelay: 0.1];
return YES;
}
Try this, 尝试这个,
Subclass UISearchBar
子类
UISearchBar
@interface CustomSearchBar : UISearchBar
and add delegate method of UITextField
并添加
UITextField
委托方法
- (BOOL)textFieldShouldClear:(UITextField *)textField
in that class. 在那个班上。 hope this will help.
希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.