9

UISearchBarには、すべてのコンテンツを一度にクリアできるX要素があります。これが発生したときに通知を受け取る方法はありますか?

UISearchBarDelegate::searchBarCancelButtonClicked「キャンセル」ボタンをタップした場合のみ起動します。

4

4 に答える 4

7

には、このUISearchBarイベントのデリゲートメソッドがありません。textDidChange:コールバックデリゲートのメソッドを実装し、空の文字列をチェックすることで、ほぼ必要なものを取得できます。

私はそれをお勧めしませんが、別の可能な方法があります。はUISearchBarUITextFieldで構成されており、ユーザーがクリアボタン(textFieldShouldClear:)をタップすると呼び出されるデリゲートメソッドがあります。の子ビューUITextFieldをトラバースすることで、を取得できます。UISearchBar

(これは派生UISearchBarクラスのコンテキストにあります)

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

    return nil;
}

ここから、デリゲート呼び出しを古いデリゲートに転送するように注意しながら、デリゲートを独自の実装に再割り当てできます。UITextFieldこのようにして、インターセプトできますtextFieldShouldClear:UISearchBarまたは、それが含まれているデリゲートであることが判明した場合は、 UITextFieldtextFieldShouldClearへの呼び出しをスウィズルすることができます:...理想的ではなく、明確ですが、技術的に実現可能です。

于 2010-11-08T05:17:30.183 に答える
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];
       }];
    }
}
于 2014-05-21T18:20:52.193 に答える
1

これが前の質問からの答えです、これはあなたが望むことを正確に行うはずです。UISearchbar clearButtonは、キーボードを強制的に表示します

于 2010-11-16T04:50:38.450 に答える
0

これが「メソッドスウィズリング」ソリューションです。

  1. の新しいカテゴリを作成しますUISearchBar-(BOOL)textFieldShouldClear:(UITextField *)textField;このカテゴリは、実行時と実行時に新しいメソッドとスウィズルメソッドを作成-(BOOL)jbm_textFieldShouldClear:(UITextField *)textFieldします。
  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. Dave DeLong- Cocoaの既存のプロトコルにメソッドを追加するにはどうすればよいですか?

  2. ニコライ・ヴラソフ-CCBottomRefreshControl

于 2016-01-08T08:05:35.750 に答える