【问题标题】:UISearchBar inside cancel button取消按钮内的 UISearchBar
【发布时间】:2014-01-27 11:53:22
【问题描述】:

我正在 XCode 5 上开发一个应用程序,但我没有找到答案。

哪种方法可以控制UISearchBar 上的“X”按钮?我试过了

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

但它不起作用

有什么线索吗?

提前致谢!

【问题讨论】:

标签: ios uiviewcontroller uisearchbar


【解决方案1】:

试试这个,

子类UISearchBar

@interface CustomSearchBar : UISearchBar

并添加UITextField的委托方法

- (BOOL)textFieldShouldClear:(UITextField *)textField

在那个班级。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    这就是你要找的方法

    - (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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-19
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多