【问题标题】:UISearchBar and event fired when 'X' element is tapped点击“X”元素时触发 UISearchBar 和事件
【发布时间】:2011-05-06 10:58:13
【问题描述】:

在 UISearchBar 上有一个 X 元素,可让您一次清除所有内容。有没有办法在发生这种情况时得到通知?

UISearchBarDelegate::searchBarCancelButtonClicked 仅在点击“取消”按钮时触发。

【问题讨论】:

标签: iphone uisearchbar uisearchbardelegate


【解决方案1】:

UISearchBar 没有此事件的委托方法。通过实现回调委托的textDidChange: 方法并检查空字符串,您几乎可以得到您想要的。

我不推荐它,但还有另一种可能的方法。 UISearchBar 由 UITextField 组成,它确实有一个委托方法,当用户点击清除按钮 (textFieldShouldClear:) 时会调用该方法。你可以通过遍历UISearchBar的子视图得到UITextField

(这是在派生的UISearchBar 类的上下文中)

- (UIView*) textField
{
    for (UIView* v in self.subviews)
    {
        if ( [v isKindOfClass: [UITextField class]] )
            return v;
    }

    return nil;
}

从这里,您可以UITextField 委托重新分配给您自己的实现,注意将委托调用转发给旧委托。这样你就可以拦截textFieldShouldClear:。或者,如果事实证明 UISearchBarUITextField 的代表,它包含您可以调整对 textFieldShouldClear 的调用:... 显然不理想,但在技术上是可行的。

【讨论】:

  • 为什么不建议实现 textDidChange 并检查空字符串?
  • 我认为这是一个很好的方法。但这并不完全是他所要求的,因为文本可以通过单击“x”以外的其他方式变为空字符串。就像退格一样。所以如果他想在点击“x”时得到通知,那么这将导致误报。
【解决方案2】:

我遇到了同样的问题,我通过使用这个函数解决了这个问题。

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{
    // This method has been called when u enter some text on search or Cancel the search.
    if([searchText isEqualToString:@""] || searchText==nil) {
        // Nothing to search, empty result.

       [UIView animateWithDuration:0.2 animations:^ {
        //Reposition search bar 
        [_searchBar setFrame:CGRectMake(230, 26, 43, 44)];
        [_searchBar setNeedsLayout];
       }];
    }
}

【讨论】:

    【解决方案3】:

    这是上一个问题的答案,这应该完全符合您的要求。 UISearchbar clearButton forces the keyboard to appear

    【讨论】:

      【解决方案4】:

      这里是“Method Swizzling”解决方案。

      1. 创建UISearchBar 的新类别。此类别在运行时在-(BOOL)textFieldShouldClear:(UITextField *)textField;-(BOOL)jbm_textFieldShouldClear:(UITextField *)textField 之间创建一个新方法和swizzle 方法。
      2. 自定义UISearchBarDelegate 的新协议以添加新方法- (void)searchBarClearButtonClicked:(id)sender;

      UISearchBar+JMBTextFieldControl.h

          @protocol UISearchBarWithClearButtonDelegate <UISearchBarDelegate>
          @optional
          - (void)searchBarClearButtonClicked:(id)sender;
          @end
      
          @interface UISearchBar (JMBTextFieldControl)
          @end
      

      UISearchBar+JMBTextFieldControl.m

          #import "UISearchBar+JMBTextFieldControl.h"
          #import <objc/runtime.h>
      
          @implementation NSObject (Swizzling)
      
          + (void)brc_swizzleMethod:(SEL)origSelector withMethod:(SEL)newSelector
          {
              Method origMethod = class_getInstanceMethod(self, origSelector);
              Method newMethod = class_getInstanceMethod(self, newSelector);
      
              if(class_addMethod(self, origSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
                  class_replaceMethod(self, newSelector, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
              else
                  method_exchangeImplementations(origMethod, newMethod);
          }
          @end
      
          @implementation UISearchBar (JMBTextFieldControl)
      
          + (void)load {
              [self brc_swizzleMethod:@selector(textFieldShouldClear:) withMethod:@selector(jbm_textFieldShouldClear:)];
          }
      
          - (id<UISearchBarWithClearButtonDelegate>)jbm_customDelegate {
              if( [[self delegate] conformsToProtocol:@protocol(UISearchBarWithClearButtonDelegate)] )
                  return (id<UISearchBarWithClearButtonDelegate>)[self delegate];
              else
                  return nil;
          }
      
          - (BOOL)jbm_textFieldShouldClear:(UITextField *)textField
          {
              if ( [[self jbm_customDelegate] respondsToSelector:@selector(searchBarClearButtonClicked:)] )
                  [[self jbm_customDelegate] searchBarClearButtonClicked:self];
      
              return [self jbm_textFieldShouldClear:textField];
          }
      
          @end
      

      参考

      1. 戴夫·德隆 - How to add a method to an existing protocol in Cocoa?

      2. 尼古拉·弗拉索夫 - CCBottomRefreshControl

      【讨论】:

        猜你喜欢
        • 2012-12-07
        • 1970-01-01
        • 1970-01-01
        • 2020-10-18
        • 1970-01-01
        • 1970-01-01
        • 2023-01-05
        • 1970-01-01
        • 2017-12-05
        相关资源
        最近更新 更多