[英]UISearchBar inside cancel button
我正在XCode 5上開發應用程序,但沒有找到答案。
哪個方法可以控制UISearchBar
上的“ X”按鈕? 我嘗試過
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
但這不起作用
有什么線索嗎?
提前致謝!
這是您要尋找的方法
- (BOOL)textFieldShouldClear:(UITextField *)textField;
但這是UITextFields的委托方法,您需要設置UISearchBar的文本字段的委托。 看到這個
在您的標頭(.h)文件中
符合代表。 <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;
}
嘗試這個,
子類UISearchBar
@interface CustomSearchBar : UISearchBar
並添加UITextField
委托方法
- (BOOL)textFieldShouldClear:(UITextField *)textField
在那個班上。 希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.