ios - Dismiss keyboard of UISearchBar with gesture recogniser after returning from modal views -
i have implemented uisearchbar application. have created programatically , using in view controller (not tableview). works fine, problem having using gesture recogniser dismiss keyboard. want keyboard dismissed when user taps screen outside of keyboard.
i know there lot of solutions out there solve problem, have rather unique problem can't figure out @ moment. have used next code implement this.
i placed line within viewdidload method of viewcontroller:
[self.view addgesturerecognizer:[[uitapgesturerecognizer alloc] initwithtarget:search action:@selector(resignfirstresponder)]];
this method works fine normally, view controller presents views modally, has next structure:
viewcontroller - modally -> view2 -modally -> view3
when view 2 , 3 dismissed , viewcontroller, gesture recogniser no longer works. have same problem when trying solution found:
uigesturerecognizer* cancelgesture; - (void) backgroundtouched:(id)sender { [self.view endediting:yes]; } #pragma mark - uisearchbardelegate -(void)searchbartextdidbeginediting:(uisearchbar *)search { cancelgesture = [uitapgesturerecognizer new]; [cancelgesture addtarget:self action:@selector(backgroundtouched:)]; [self.view addgesturerecognizer:cancelgesture]; } -(void)searchbartextdidendediting:(uisearchbar *)search { if (cancelgesture) { [self.view removegesturerecognizer:cancelgesture]; } }
i added these main viewcontroller , 1 time again worked fine start with, after returning modal views gesture no longer worked.
any help appreciated please!
edit 1:
so while testing found similar effect beingness had when pressing search button on keyboard. utilize next code dismiss keyboard 1 time search has been pressed:
- (void) searchbarsearchbuttonclicked:(uisearchbar *)search { [search resignfirstresponder]; }
same effect here, works fine when first start app , searching, after coming screen modally presented view controllers pressing come in no longer dismisses keyboard.
edit 2:
i tried adding piece of code in searchbarsearchbuttonclicked delegate method:
- (void) searchbarsearchbuttonclicked:(uisearchbar *)search { if ([search isfirstresponder]) { [search resignfirstresponder]; nslog(@"text field first responder"); } else { [search becomefirstresponder]; [search resignfirstresponder]; nslog(@"text field not first responder"); } }
and both times prints text field first responder. after returning modal views , clicking search bar 1 time again still says first responder not dismiss odd. insight helpful, thanks.
instead of adding , removing gestures in -(void)searchbartextdidbeginediting:(uisearchbar *)search {
, -(void)searchbartextdidendediting:(uisearchbar *)search {
in viewwillappear' (add here) and
viewdiddisappear' (remove here).
and assuming cancelgesture
property retain
attribute. while adding gesture instead
uitapgesturerecognizer *gesture = [uitapgesturerecognizer new]; [gesture addtarget:self action:@selector(backgroundtouched:)]; self.cancelgesture = gesture; [self.view addgesturerecognizer:self.cancelgesture];
and remember release cancelgesture
in dealloc
.
ios uigesturerecognizer uisearchbar uitapgesturerecognizer
No comments:
Post a Comment