-1

Simple question: how can I resign my textfield if my "done" key is Search. Essentially, what do I do if the user does not want to search, but instead wants to cancel...

thanks

Apollo
  • 8,874
  • 32
  • 104
  • 192
  • Put a bar button in the navigation bar, which hides the keyboard by just resigning the first responder. – Parth Bhatt Mar 26 '12 at 04:47
  • same question like this:-http://stackoverflow.com/questions/8558339/how-i-dismiss-keyboard-on-tapping-search-button-on-keyboard-regarding-uisearchba – Leena Mar 26 '12 at 04:48

2 Answers2

2

You can use: a cancel button for SearchBar and need to implement this SearchBar delegate :

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    isSearching = NO; //This is a flag which specifies if searching is going on or not.
    searchBar.text = @""; //Clears out search bar text
    [self resetSearch]; //Reset search resets all the flags and variables.
    [self.leadsTable reloadData]; //Reloads the tableView
    [searchBar resignFirstResponder]; //Resigns responder from Search bar
}

This is a proper way to resign the responder if user doesn't want to search.

Look at how you can add an in-built Cancel button in UISearchBar. Check the property "Shows Cancel Button" (Red Arrow highlight)

enter image description here

EDIT:

STEP-1: Check conditionally whether your textField's text is blank? If so resignFirstResponder for the TextField. You need to set the Delegate to self for the UITextField using code:

txtField.delegate = self;

Or you can do the same from the interface builder by connecting TextField's delegate to File's Owner.

STEP-2: You need to implement this delegate method.

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
   if([textField.text isEqualToString:@""])
   {
        [textField resignFirstResponder];
   }
    return YES;
}

EDIT-1:

Now create a UIToolbar with one bar button labeled 'Cancel'

Once you have done that:

You can write an button click event for UIToolBar:

-(IBAction)cancelClicked:(id)sender
{
    [txtField resignFirstResponder];
}

Once you have done that you can now just write:

txtField.inputAccessoryView = toolbarOutlet;

Hope this helps you.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • this is not what I'm looking for as the textField is inside of a UITableView cell, not a UISearchBar. Thank you for your answer though. – Apollo Mar 26 '12 at 15:47
  • @derek.lo: Any specific reason to have a UITextField and not a UISearch bar under a UITableViewCell? – Parth Bhatt Mar 26 '12 at 15:49
  • The textfield is not meant to search through results in a tableview, it is meant to send a request for an item in DB. Therefore it just looks better in a cell. – Apollo Mar 26 '12 at 15:51
  • Then why do you show a search button in keyboard when you have your textField in focus and keyboard is being appeared? – Parth Bhatt Mar 26 '12 at 15:52
  • @derek.lo: Please do check **EDIT** section of my answer. I have edited my answer and made it more feasible as in your case you have a `UITextField` and not a `UISearchBar`. – Parth Bhatt Mar 26 '12 at 16:00
  • it would be most helpful if you could go over this: Use the inputAccessoryView of the text field to display a separate cancel button in a toolbar above the keyboard. – Apollo Mar 26 '12 at 16:02
  • @derek.lo oh yes, Refer to **Ermiar**'s answer in my link: http://stackoverflow.com/questions/5591792/how-to-get-keyboard-with-next-previous-and-done-button – Parth Bhatt Mar 26 '12 at 16:04
  • @derek.lo: Please refer to **EDIT-1** section of my code to get an idea on how to use `inputAccessoryView` to attach a UIToolbar with Cancel button. – Parth Bhatt Mar 26 '12 at 16:10
  • this is clearly now the best answer. Thanks for your advice and time – Apollo Mar 26 '12 at 16:14
1

The text on the return button is irrelevant to the discussion. You want to know how to resign first responder without pressing the return button, and there are a few ways to do it.

  1. Use the inputAccessoryView of the text field to display a separate cancel button in a toolbar above the keyboard.
  2. Use a tap gesture recognizer on the field's superview to recognize when the user taps outside the field, and call [self.view endEditing:YES] (where self is your view controller). This will cause the first responder to resign. (This is very finicky in a scroll view.)
  3. Swap out the rightBarButtonItem of the current view controller for a cancel bar button item while editing, assuming you have a UINavigationBar on screen at the time. When editing ends, swap back in the regular right bar button item, if any.
warrenm
  • 31,094
  • 6
  • 92
  • 116