2014-01-27 118 views
0

我正在XCode 5上開發一個應用程序,我沒有找到答案。UISearchBar裏面取消按鈕

my UISearchBar

哪一種方法是控制在該UISearchBar 「X」 按鈕的人嗎?我試着用

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 

但它不工作

任何線索?

在此先感謝!

+0

請參閱本http://stackoverflow.com/questions/15847745/right-align-magnifying-glass-icon-in-uisearchbar – Spynet

回答

0

試試這個,

子類UISearchBar

@interface CustomSearchBar : UISearchBar 

,並添加委託方法。希望這會有所幫助。

0

這是你正在尋找

- (BOOL)textFieldShouldClear:(UITextField *)textField; 

方法但這是UITextFields委託方法和需要設置的UISearchBar的文本框的委託。看到這個

在你的頭文件(.h)

符合代表。在該類的UITextField

- (BOOL)textFieldShouldClear:(UITextField *)textField 

<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; 
} 
相關問題