Reputation: 596
My App is having a search bar for searching records from the table view,which is populated by sqlite DB. My problem is that when the view opens the "cancel" button is not enabled and also I cant touch on that, just like a image only.It is there but no action is with that. when we click on that search bar text the cancel button will be changed to "done" it is enabled one. so here is my code
this is my search bar view,see that cancel button.It is not enabled
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
//[newSearchBar setShowsCancelButton:YES animated:YES];
newSearchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
NSLog(@"search begin edit") ;
//searchString = searchBar.text;
//NSLog(@"print did edit searchstring : %@", searchString) ;
for(UIView *view in [searchBar subviews])
{
//shareItemId =newSearchBar.text;
if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
[(UIBarItem *)view setTitle:@"Done"];
}
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
NSLog(@"searchBarTextDidEndEditing:");
[searchBar resignFirstResponder];
//[self dismissModalViewControllerAnimated:YES];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"searchBarSearchButtonClicked");
searchString = searchBar.text;
NSLog(@"search %@", searchBar.text);
[newSearchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
//[self dismissModalViewControllerAnimated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
NSLog(@" searchBarCancelButtonClicked");
[searchBar resignFirstResponder];
shareItemName =newSearchBar.text;
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
NSLog(@"searchBarShouldBeginEditing");
[newSearchBar setShowsCancelButton:YES animated:YES];
return YES;
}
These are my delegates for that
Please check my code and give me the answer. I need to enable the "Cancel" button when the view is loaded and it action will be go back to previous view
I need like this
Or else how can I add a another cancel button on exciting cancel button.so that I can enable that.please give me all the details
Upvotes: 6
Views: 14630
Reputation: 56
This works to reenable the cancel button as of iOS 8:
private func enableButtonsInSubviews(view: UIView) {
if let view = view as? UIButton {
view.enabled = true
}
for subview in view.subviews {
enableButtonsInSubviews(subview)
}
}
Upvotes: 1
Reputation: 5973
Here's what I did to always enable the cancel button, even when the search field is not first responder.
I'm calling this method whenever I call resignFirstResponder
on the search field
- (void)enableCancelButton {
for (UIView *view in self.searchBar.subviews) {
if ([view isKindOfClass:[UIButton class]]) {
[(UIButton *)view setEnabled:YES];
}
}
}
This works, but I'm not sure whether it will pass App Store verification yet, so use it at your own risk. Also, this probably only works if the cancel button is the only button you are using with the search field.
Upvotes: 3
Reputation: 12106
You need to set the UISearchDisplayController to be ACTIVE, like this:
[mySearchDisplayController setActive:YES animated:YES];
or more simply:
mySearchDisplayController.active = YES;
Upvotes: 6
Reputation: 6118
My guess is that Apple made the UISearchBar in a way that the cancel button is disabled if the search text field is empty or not first responder.
This is make sense because you should not use the "Cancel" button to other purpose than actually canceling the search. and since there is no search to cancel - the button is disabled.
If you still want that the button will be active immediately when the view is presented, you can call at viewWillAppear:
to [mySearchBar becomeFirstResponder];
This will cause to the keyboard to appear and the button will be enabled. And then if the user hit cancel you can intercept it to go back to the previous view. (I'm not sure if apple will like this behavior).
Sample code:
-(void) viewWillAppear : (BOOL) animated
{
[super viewWillAppear:animated];
// Make keyboard pop and enable the "Cancel" button.
[self.mySearchBar becomeFirstResponder];
}
Upvotes: 4