UISearchBarには、すべてのコンテンツを一度にクリアできるX要素があります。これが発生したときに通知を受け取る方法はありますか?
UISearchBarDelegate::searchBarCancelButtonClicked
「キャンセル」ボタンをタップした場合のみ起動します。
UISearchBarには、すべてのコンテンツを一度にクリアできるX要素があります。これが発生したときに通知を受け取る方法はありますか?
UISearchBarDelegate::searchBarCancelButtonClicked
「キャンセル」ボタンをタップした場合のみ起動します。
には、このUISearchBar
イベントのデリゲートメソッドがありません。textDidChange:
コールバックデリゲートのメソッドを実装し、空の文字列をチェックすることで、ほぼ必要なものを取得できます。
私はそれをお勧めしませんが、別の可能な方法があります。はUISearchBar
UITextFieldで構成されており、ユーザーがクリアボタン(textFieldShouldClear:
)をタップすると呼び出されるデリゲートメソッドがあります。の子ビューUITextField
をトラバースすることで、を取得できます。UISearchBar
(これは派生UISearchBar
クラスのコンテキストにあります)
- (UIView*) textField
{
for (UIView* v in self.subviews)
{
if ( [v isKindOfClass: [UITextField class]] )
return v;
}
return nil;
}
ここから、デリゲート呼び出しを古いデリゲートに転送するように注意しながら、デリゲートを独自の実装に再割り当てできます。UITextField
このようにして、インターセプトできますtextFieldShouldClear:
。UISearchBar
または、それが含まれているデリゲートであることが判明した場合は、 UITextField
textFieldShouldClearへの呼び出しをスウィズルすることができます:...理想的ではなく、明確ですが、技術的に実現可能です。
同じ問題があり、この関数を使用してこの問題を解決しました。
- (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];
}];
}
}
これが前の質問からの答えです、これはあなたが望むことを正確に行うはずです。UISearchbar clearButtonは、キーボードを強制的に表示します
これが「メソッドスウィズリング」ソリューションです。
UISearchBar
。-(BOOL)textFieldShouldClear:(UITextField *)textField;
このカテゴリは、実行時と実行時に新しいメソッドとスウィズルメソッドを作成-(BOOL)jbm_textFieldShouldClear:(UITextField *)textField
します。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
参照
Dave DeLong- Cocoaの既存のプロトコルにメソッドを追加するにはどうすればよいですか?
ニコライ・ヴラソフ-CCBottomRefreshControl