Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resign keyboard when losing focus on uisearchbar

I'm making an UISearchBar option in the navigationbar of my app. My app consists of multiple views and subviews. I have this mainview which has 3 other views on himself. one of it is empty (for now) the other 2 have tableviews on them.

I want my keyboard to show when I'm searching and hide when i'm doing the actual search or when i touch/click outside the uisearchbar. Im using the searchbardelegate as is required.

Im able to hide the keyboard using [searchBar resignFirstResponder] in the following ways.

  • When im pressing the return key.
  • When i cancel search manually
  • When i press any of the keyboard buttons to search or cancel.
  • When i touch an empty part of the screen using

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {    
        UITouch *touch = [[event allTouches] anyObject];
        if ([mySearchBar isFirstResponder] && [touch view] != mySearchBar) {
            [mySearchBar resignFirstResponder];
        }
        [super touchesBegan:touches withEvent:event];
    }
    

What i not seem to be able to do is make it respond to touching one of my 2 tableviews. Or when im refilling the mainview to contain something different entirely.

Ive tried changing the touchesbegan method to resign the searchbar when touching the tableviews but it hasnt worked so far.

I've tried several other things found by my dear friend mr. google, but it all seems to be something other then I need.

Anyone have any ideas of what I might do to fix this problem?

EDIT: It appears so that, when using breakpoints, touchesbegan-method does respond to the backgroundview but it doesnt respond when i touch either of the tableviews or the navigationbar (containing the uisearchbar).

like image 558
Totumus Maximus Avatar asked Sep 20 '11 08:09

Totumus Maximus


1 Answers

Solved it!

- (BOOL) searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self.myViewController1.customView1 setUserInteractionEnabled:NO];
    [self.myViewController2.customView2 setUserInteractionEnabled:NO];   
    [searchBar setShowsCancelButton:YES animated:[mySettings animation]];
    return YES;   
}

I started by shutting down the userInteraction on my 2 subviews, at the moment I start using the searchBar.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{    
    UITouch *touch = [[event allTouches] anyObject];
    if ([self.mySearchBar isFirstResponder] && [touch view] != self.mySearchBar) 
    {
        [self.mySearchBar resignFirstResponder];
        [self.myViewController1.customView1 setUserInteractionEnabled:YES];
        [self.myViewController2.customView2 setUserInteractionEnabled:YES];
    }
    [super touchesBegan:touches withEvent:event];
}

Then when I click/tap/touch outside the searchBar I first resign the keyboard which is first responder still AND AFTER that I set userInteraction back on for the 2 subviews. The order of doing this is vital!

This piece of code allows u to resign the keyboard in a single mainViewController even when it is crowded with a massload of subviews.

like image 88
Totumus Maximus Avatar answered Nov 05 '22 11:11

Totumus Maximus