UISearchBar 和事件在“X”时触发元素被点击

发布于 2024-09-30 17:02:18 字数 147 浏览 1 评论 0原文

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

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

On the UISearchBar, there's an X element that allows you to clear all of the contents at once. Is there a way to get notified when this happens?

UISearchBarDelegate::searchBarCancelButtonClicked is fired only when the "Cancel" button is tapped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

德意的啸 2024-10-07 17:02:18

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:。或者,如果事实证明 UISearchBar 是它包含的 UITextField 的委托,您可以混合对 textFieldShouldClear 的调用:...不理想,显然,但技术上可行。

The UISearchBar doesn't have a delegate method for this event. You can nearly get what you want by implementing the textDidChange: method of the callback delegate and checking for an empty string.

I don't recommend it, but there is another possible way. The UISearchBar is composed of a UITextField, which does have a delegate method that is called when the user taps the clear button (textFieldShouldClear:). You can get the UITextField by traversing the UISearchBar's child views:

(this is in the context of a derived UISearchBar class)

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

    return nil;
}

from here, you could re-assign the UITextField delegate to your own implementation, taking care to forward delegate calls to the old delegate. This way you could intercept textFieldShouldClear:. Or if it turns out the UISearchBar is the delegate for the UITextField it contains you could swizzle the call to textFieldShouldClear:... Not ideal, clearly, but technically feasible.

回忆躺在深渊里 2024-10-07 17:02:18

我遇到了同样的问题,我通过使用此功能解决了这个问题。

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

I had the same issue and I solved this issue by using this function.

- (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];
       }];
    }
}
穿透光 2024-10-07 17:02:18

这是上一个问题的答案,这应该完全符合您的要求。 UISearchbar clearButton 强制键盘显示

Here is an answer from a previous question, this should do exactly what you want. UISearchbar clearButton forces the keyboard to appear

仄言 2024-10-07 17:02:18

这是“方法混合”解决方案。

  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. < p>戴夫·德隆 -
    如何向现有协议添加方法可可?

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

Here is "Method Swizzling" solution.

  1. Create a new Category of UISearchBar. This category create a new method and swizzle method between -(BOOL)textFieldShouldClear:(UITextField *)textField; and -(BOOL)jbm_textFieldShouldClear:(UITextField *)textField in runtime.
  2. Customize a new Protocol of UISearchBarDelegate in order to add a new method - (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

Reference

  1. Dave DeLong -
    How to add a method to an existing protocol in Cocoa?

  2. Nikolay Vlasov - CCBottomRefreshControl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文