user1242108
user1242108

Reputation: 123

UISearchBar cancel button Xcode 4.3?

How do I get the cancel button to only appear when the user starts entering text into the UISearchBar?

I tried this

- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar {
self.searchBar.showsCancelButton = YES;
}

But it doesn't work, I don't think there's anything wrong with with setting it to "showsCancelButton" because even when I say

NSLog(@"Typing");

It won't print out to the screen. Is there another method in Xcode 4.3 that can do this? Is there also a method that knows if the cancel button has been pressed?

Upvotes: 0

Views: 2932

Answers (3)

Rob
Rob

Reputation: 5954

Your method is not returning a BOOLean value, 'YES.' This should work:

// show search bar's cancel button when editing
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {

    searchBar.showsCancelButton = YES;

    return YES;
}
// hide search bar's cancel button when not editing
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {

    searchBar.showsCancelButton = NO;

    return YES;
}

Upvotes: 1

shebi
shebi

Reputation: 719

Try this:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar {
  aSearchBar.showsCancelButton = YES;
}

Upvotes: 0

fbernardo
fbernardo

Reputation: 10124

Are you sure that the class is the delegate of searchBar? Try setting self.searchBar.delegate=self; in the viewDidLoad method.

Upvotes: 1

Related Questions