5

Something peculiar is happening in my code with my UISearchBar and I can not figure out what is happening. In my application I have a view controller set up as follows

View Controller A ---> View Controller B

On View Controller A, I have a toolbar that contains a "page curl" button. Once this button is tapped, I perform a modal segue with a page curl transition style and I display View Controller B (this gives the user the effect that View Controller B is beneath View Controller A when in reality they are two distinctly seperate View Controllers). On View Controller B, I have a button called "Find Near Address." When a user taps "Find Near Address" I am looking for two actions to occur in seamless succession:

  • First, I would like to perform an unwind segue (undo the page curl transition and curl View Controller A back onto View Controller B)
  • Second, Once View Controller B has disappeared and View Controller A is the main view again, I would like for a UISearchBar on View Controller A to become the first responder, meaning a flashing cursor should appear in the search bar and a keyboard should pop up.

So far, I have been able to perform the unwind segue successfully but when trying to set my UISearchBar on View Controller A as first responder, the keyboard is not popping up! I am completely lost and I do not know why this is happening.

The approach I have taken is simple. The unwind segue on view Controller B returns and steps into a function call on View Controller A called (IBAction)returnToMainView:(UIStoryboardSegue *)segue. Now from within this function, I call [self FindNearAddress] and below is my FindNearAddress function:

- (void)FindNearAddress {
    [self.theSearchBar becomeFirstResponder];
}

I have also implemented the following UISearchBarDelegate methods

- (void)searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
    [self.theSearchBar setShowsCancelButton:YES animated:YES];
    self.theSearchBar.text = @"";
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)theSearchBar {
    self.theSearchBar.text=@"Please enter a custom address..";
    [self.theSearchBar setShowsCancelButton:NO animated:YES];
    [self.theSearchBar resignFirstResponder];
}

Can anyone please help me/point out what I am doing wrong? I would like to point out that I added a test button on View Controller A that calls my "FindNearAddress" function and this succeeded in making my UISearchBar first responder and bringing up the keyboard. The keyboard only fails to pop up when I am returning (unwinding) from View Controller B, the first responder gets set but the keyboard does not pop up for some strange reason.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Abhay Curam
  • 75
  • 2
  • 6
  • check this --> `self.theSearchBar.delegate = self;` – Bala Aug 07 '13 at 05:19
  • I have that code in my viewWillAppear method, however I have noticed that when unwinding the segue from View Controller B to View Controller A, View Controller A's viewWillAppear method is not getting called. should I place it elsewhere? Like maybe in my "FindNearAddress" function call? – Abhay Curam Aug 07 '13 at 05:29
  • I tried placing that line in the function call as well and it did not do anything, keyboard still does not pop up. Not sure if you have any idea where else I could put it.. – Abhay Curam Aug 07 '13 at 05:37

0 Answers0