Ladessa
Ladessa

Reputation: 1003

SearchBar doesn't work when I press a keyboard button

I'm trying to implement a searchbar in my project.

I have already created the SearchBar outlet, have set the delegate, but when I press any keyboard button, the value from this button does not appear in UISearchBar

Can anybody tell me why?

  - (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar {

     // When the search button is tapped, add the search term to recents and conduct the search.
     NSString *searchString = [aSearchBar text];
     NSLog(searchString);
 }

Upvotes: 1

Views: 344

Answers (3)

Ladessa
Ladessa

Reputation: 1003

I have missed the code below in appDelegate , applicationDidFinishLauching

 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];

Now the SearchBar is working fine!

Upvotes: 0

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21966

Maybe you forgot to implement this method:

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    return YES;
}

By returning YES you "say" that you accept editing.

EDIT

You don't even need to implement this method, by default a UISearchBar accepts editing.
No extra method is need to be implemented.

To be sure I've just did the following:

  • Dragged a search bar in the xib file;
  • Bound via xib the delegate to my view controller;
  • Implemented searchBarSearchButtonClicked: and put a breakpoint on it.

Works perfectly.Make sure that you have done all in the right way.

Upvotes: 1

Ladessa
Ladessa

Reputation: 1003

I have forgot this method

-(BOOL) searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    return YES;
}

Upvotes: 0

Related Questions