UISearchBar and the event that was fired when the X element was deleted

The UISearchBar has an X element that allows you to immediately clear all content. Is there a way to get notified when this happens?

UISearchBarDelegate::searchBarCancelButtonClicked only starts when the "Cancel" button is clicked.

+8
iphone uisearchbar uisearchbardelegate
Nov 08 2018-10-10T00:
source share
4 answers

UISearchBar does not have a delegation method for this event. You can almost get what you want by implementing the callback delegate textDidChange: method and checking for an empty string.

I do not recommend this, but there is another possible way. UISearchBar consists of a UITextField that has a delegate method that is called when the user clicks the clear button ( textFieldShouldClear: . You can get a UITextField by going through the child views of the UISearchBar :

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

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

from here you can re-assign the UITextField delegate UITextField your own implementation, taking care to redirect the delegate calls to the old delegate. That way you can intercept textFieldShouldClear: Or, if it turns out that the UISearchBar is a delegate for UITextField , it contains that you can swizzle a call to textFieldShouldClear: ... Not perfect, understandable, but technically feasible.

+6
Nov 08 '10 at 5:17
source share
— -

I had the same problem and solved this problem 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]; }]; } } 
+2
May 21 '14 at 18:20
source share

Here is the answer from the previous question, this should do exactly what you want. UISearchbar clearButton makes the keyboard display

+1
Nov 16 '10 at 4:50
source share

Here is a Swizzling solution.

  • Create a new UISearchBar category. This category creates a new method and a swizzle method between -(BOOL)textFieldShouldClear:(UITextField *)textField; and -(BOOL)jbm_textFieldShouldClear:(UITextField *)textField at runtime.
  • Configure the new UISearchBarDelegate protocol 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 

Link

0
Jan 08 '16 at 8:05
source share



All Articles