iphone - Dismissing keyboard from UISearchBar when the X button is tapped -


i'm using uisearchbar (but not searchdisplaycontroller that's typically used in conjunction) , i'd dismiss keyboard when hit 'x' button.

i've followed tomswift's suggestion on getting called when 'x' tapped , works great. resigning first responder text field , invoking in uisearchbar instance, both resignfirstresponder, won't make keyboard go away.

is there way rid of keyboard when user has tapped x button?

here's did 'clear' notify:

- (void)viewdidload: {     (uiview* v in searchbar.subviews)     {         if ( [v iskindofclass: [uitextfield class]] )         {             uitextfield *tf = (uitextfield *)v;             tf.delegate = self;             break;         }     }     } 

then have class setup implement both uisearchbardelegate , uitextfielddelegate.

having class serve textfield delegate allows me call:

- (bool)textfieldshouldclear:(uitextfield *)textfield {      [textfield resignfirstresponder];      [self.searchbar resignfirstresponder];      return yes; } 

i've tried can think of. last thing i'm trying find way issue 'searchbarcancelbuttonclicked' uisearchdelegate invoke on controller class not i'm sure how since uisearchbar doesn't seem have direct methods invoke name.

update:

well, total hack able make work. code invokes handler cancel button. make work had invoke selector delay, , i'm not sure why had be. also, had write accessor cancel button did text field.

again, total hack. i'm not sure i'd myself in app.

// in context of search bar - (uibutton*) cancelbutton {     (uiview* v in self.subviews)     {         if ( [v iskindofclass: [uibutton class]] )             return (uibutton*)v;     }      return nil; }  // textfield delegate callback - (bool)textfieldshouldclear:(uitextfield *)textfield {     [textfield resignfirstresponder];      uibutton* cb = _searchbar.cancelbutton;      nsobject* target = [[cb alltargets] anyobject];      nsarray* actions = [cb actionsfortarget: target forcontrolevent:uicontroleventtouchupinside];      nsstring* selectorname = [actions  objectatindex:0];      sel selector = nsselectorfromstring( selectorname );      [target performselector: selector withobject: cb afterdelay: 0.1];      return yes; } 

original answer:

how clear 'x' button display in first place? in test case dont see displaying...

try doing resignfirstresponder on searchbar, not textfield.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -