UISearchDisplayController - Method for when cancel button is clicked - iphone

I have a UISearchDisplayController that shows the cancel button. I would like to call a method when a user clicks the cancel button. Is there a way to do this?

You can use the following searchbar delegate method,
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
// DO ur operations
}

For UISearchDisplayController, the following 2 methods can be considered.
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
NSLog(#"WillEndSearch");
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
NSLog(#"DidEndSearch");
}

Related

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?
Are you sure that the class is the delegate of searchBar? Try setting self.searchBar.delegate=self; in the viewDidLoad method.
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;
}
Try this:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar {
aSearchBar.showsCancelButton = YES;
}

Which TextFieldDelegate method should be called?

In my application , I want to show one popover view, whenever I am clicking in textfield.
Which text field delegate method should I call?
you have to write your functionality in
-(void)textFieldDidBeginEditing:(UITextField *)textField{
// became first responder
}
Use on the UITextFieldDelegate protocol......
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"Editing Should Editing"); // SAVE PROCEDURE
}

Search Bar Cancel Button is Not Working

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
You need to set the UISearchDisplayController to be ACTIVE, like this:
[mySearchDisplayController setActive:YES animated:YES];
or more simply:
mySearchDisplayController.active = YES;
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];
}
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.
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)
}
}

Cancel button click highlight after showing UIActionView on iphone

I have a view that has a button which brings up a UIActionSheet pop up on click. If the user clicks cancel, the original button that brought up the action sheet is still highlighted as if it is being clicked. How do I reset the state after user cancels the action?
Use one of the protocol methods to change the state of the button that was clicked:
#protocol UIActionSheetDelegate <NSObject>
#optional
// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)actionSheetCancel:(UIActionSheet *)actionSheet;
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet; // before animation and showing view
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet; // after animation
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex; // before animation and hiding view
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex; // after animation
#end
Sorry about my false assumption that you were using UIButton. Here should be what you need:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//your implementation here
....
//Then deselect the row so it quits the highlighted state
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

make return button on keyboard perform an action

I have a searchbox. I want to make it so when the user pushes search on there keyboard it performs and IBAction. How can i link that key to the -(IBAction)Method.
There one delegate method which is fired when search button is pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
//CALL YOUR IBACTION METHOD HERE
}
-Happy Coding
(BOOL)textFieldShouldReturn:(UITextField *)textField{
[self yourButtonName:nil];
}