Why searchBarTextDidEndEditing not called? - ios

I have this code:
extension VC : UISearchBarDelegate
{
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
print("searchBarTextDidEndEditing")
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
// searchBar.resignFirstResponder()
}
}
searchBarCancelButtonClicked has fired when clicked on search , but searchBarTextDidEndEditing has not fire until I call searchBar.resignFirstResponder()
I read a lot of questions and answers about that, but I did not understand why is that happen, and what is resignFirstResponder() do here?

As stated in the documents resignFirstResponder
Notifies this object that it has been asked to relinquish its status as first responder in its window.
Basically this function call tells your input field that it should not be getting any more inputs from the user or the input action is finished for this input field. Thus your searchBarTextField ends it's editing status and searchBarTextDidEndEditing is called.
If you'd like to learn more about what a firstResponder is then you may want to checkout this part of the documentation.

This may be useful to some like myself wondering why similar delegate methods are not firing. Check you have set up all appropriate delegates for example:
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self

Related

UISearchBar searchBarCancelButtonClicked not being called using Swift 3

For some reason when I click the circle x cancel button in a UISearchBar the searchBarCancelButtonClicked event is not firing, it worked in a swift 2 project but not in this swift 3 one.
I am now extending my view controller instead of the inline class way but I believe that is working as the searchBarSearchButtonClicked event does work. Here is what I have so far:
extension MyViewController: UISearchBarDelegate {
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("here?")
searchBar.resignFirstResponder()
handleCancelSearch()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
if let searchText = searchBar.text {
performSearchUsing(term:searchText)
}
}
}
The print is not logged and the function not called. Am I missing something silly?
Maybe you are missing something silly as I did, the "circle x" is not actually the cancel button, and I thought that too, the cancel button comes disabled by default, you can activate it via storyboard on the attributes of the searchBar or you can do it programmatically with:
searchBar.showsCancelButton = true
After that, the method should work.
If any one still looking for the option how act on "X" button. use below method which is available in
UISearchBarDelegate
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
if searchText.count == 0
{ // Hide content
}
}
Note : it is not cannel button its clear text button which is visible after typing something in searchBar

resignFirstResponder and endEditing (even global calls) are not hiding the keyboard

I have a ViewController set as a delegate for a UITextField. No matter what I try, I can not get the keyboard to hide when calling textFieldShouldReturn.
My viewDidLoad method:
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(newModelField)
newModelField.delegate = self
self.presetTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "presetCell")
//self.view.becomeFirstResponder()
}
and my textFieldShouldReturn method:
func textFieldShouldReturn(textField: UITextField) -> Bool {
//UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
if(textField.text! != ""){
items.append(textField.text!)
presetTableView.reloadData()
textField.text! = ""
//self.view.resignFirstResponder()
//self.addView.resignFirstResponder()
//textField.endEditing(true)
//self.view.endEditing(true)
UIApplication.sharedApplication().delegate?.window?!.endEditing(true)
return false
}
return true
}
You can see from my commented out attempts that I have tried a whole host of different fixes. I know that textFieldShouldReturn is being called after debugging with print statements. I can not figure out why the keyboard won't hide.
I tried swapping the return true and false statements. I tried setting the delegate from the storyboard. I tried deleting the textField from storyboard, cleaning, and adding it back in. I tried all top level calls to resignFirstResponder and endEditing as well as calls to these functions for the textfield and the view.
To dismiss the keyboard, send the resignFirstResponder message to the text >field that is currently the first responder. Doing so causes the text >field object to end the current editing session (with the delegate >object’s consent) and hide the keyboard.
yourTextField.resignFirstResponder()
I tried using resignFirstResponder() on textFiled and also used endEditing(true) on my view but I did this small mistake of implementing this other delegate and was returning false
func textFieldShouldEndEditing(textField: UITextField) -> Bool { //delegate method
return true
}

Dismiss UISearchBar keyboard without affecting cancel button

I am trying to reproduce the functionality that can be seen in the contacts app on the iphone. I have a UISearchBar that dismisses the keyboard when the search button is clicked. This however deactivitates the cancel button and it requires 2 touches to activate. On the contacts app it is not deactivated when the search button is clicked and the keyboard is dismissed.
So what I am asking is how to dismiss the keyboard without deactivating the cancel button on the uiSearchBar?
I have tried
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
//Some other code
//I have Tried
//Attempt 1
self.searchBar.endEditing(true)
//Attempt 2
self.searchBar.resignFirstResponder()
//Attempt 3
var textFieldInsideSearchBar = searchBar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar.endEditing(true)
}
Delegate method parses you searchBar, so you do not have to use self.searchBar, that might be one of the issues. I usually use logic from your "Attempt 2".
You can try to implement this:
func searchBarTextDidEndEditing(_ searchBar: UISearchBar)
And call searchBar.resignFirstResponder().
If it does not work, then try to implement this method and return true:
func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool
If you are using UISearchBar in combination with UISearchDisplatController, then try this method on searchDisplayController:
func setActive(_ visible: Bool, animated animated;: Bool)
It quite bit tricky.
Try,
[self.searchBar resignFirstResponder];
[(UIButton *)[self.searchBar valueForKey:#"_cancelButton"] setEnabled:YES];

How to dismiss keyboard with multiple UITextField

I'm a noob here and in iOS world. I am having trouble dismiss keyboard on a specific case in my very simple todo list iOS app.
I'd like the keyboard to get dismiss when user taps anywhere outside the current text field or the keyboard itself. So far, I got the keyboard dismisses just fine (thanks to you guys here in stack overflow) when user taps on the UITableView, or most element on my app. HOWEVER, when user taps on another UITextField, the keyboard does not go away.
FYI, here's the list of existing threads I researched so far but have yet to solve this issue.
1) How to dismiss keyboard iOS programmatically
2) Resigning First Responder for multiple UITextFields
3) Dismissing the First Responder/Keyboard with multiple Textfields
4) (a few more at least but I lost track :( )
Here's what I did so far:
(in viewDidLoad())
// Add 'tap' gesture to dismiss keyboard when done adding/editing to-do item
var tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapOutside:")
tap.cancelsTouchesInView = true
self.view.addGestureRecognizer(tap)
func tapOutside(tapOutside: UIGestureRecognizer) {
// Dismiss keyboard
self.view.endEditing(true)
}
#IBAction func EditingDidBegin(sender: UITextField) {
// Highlight the text field which user is editing
self.highlightTextField(sender, highlight: true)
}
#IBAction func EditingDidEnd(sender: UITextField) {
// Undo text field highlight
self.highlightTextField(sender, highlight: false)
self.view.endEditing(true) // try this option and not working
self.setEditing(false, animated: true) // try this option and not working
sender.resignFirstResponder() // try this option and not working
UIApplication.sharedApplication().becomeFirstResponder() // try this option and not working
... // below is my code to update the todo item
}
I also tried to print out all subviews.isFirstResponder() of my view. All of it return false. I also tried override touchesBegan of my UIViewController, and inside it just calls self.view.endEditing(true) and call its super's. This also does not work.
Please help. :(
TIA!
UPDATE:
You guys are awesome! :D I got it working now thanks to you guys. There were several mistakes / messed up as I'm learning new framework. So here's what I did.
1) I did not set UITextField delegate correctly.
Mistake: I ctrl-draged textfield in xcode and link my viewController as delegate and thought that should work out. I will still need to research and understand better why.
Solution: I removed that ctrl-drag link and explicitly call myTextField.delegate = self in tableView:cellForRowAtIndexPath. And that did it. Thanks #Sidewalker
2) Mistake: I have a mixed of textFieldShouldBeginEditing, etc. and #IBAction func EditingDidBegin. So I got myself into the situation where textFieldShouldBeginEditing got the call, but EditingDidBegin did not get call.
Solution: Once I set the delegate = self explicitly and stick with implementing textField... methods and not use any #IBAction for textField, things just work.
Here's one option... We're going to add a boolean flag to determine whether or not we're in a textField when an edit attempt for another textField begins
Make your class adhere to UITextFieldDelegate
class MyClass: UIViewController, UITextFieldDelegate
Don't forget to set the delegate, we'll add the flag as well
myTextField.delegate = self
var inField = false
Implement "textFieldShouldBeginEditing" and "textFieldDidBeginEditing"
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if inField {
inField = false
return false
}
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
inField = true
}
I prefer tracking things like this rather than identifying subviews as it allows the flag to be utilized elsewhere and cuts down code complexity.
Well the keyboard isn't going away because it doesn't expect to have to. The new UITextField is just becoming the first responder while the other resigns. If you don't want a textField to become the first responder if another is already, you're going to have to cut it off before it gets the chance to. I would try to implement textFieldShouldBeginEditing and figuring out the logic there.
I'm not in love with the way this looks but this should do something along those lines.
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
for subView in self.view.subviews{
if(subView.isKindOfClass(UITextField)){
if(subView.isFirstResponder()){
subView.resignFirstResponder();
return false;
}
}
}
return true;
}
First set all the UITextField (your are creating) delegate as self and create one UITextField member variable. Now implement "textFieldDidBeginEditing" delegate method and assign the textfield to your member UITextField variable. As given below
func textFieldDidBeginEditing(textField: UITextField) {
yourMemberVariable = textField;
}
So now whenever you want to dismiss the keyboard call the dismiss method on "yourMemberVariable" object. It should work !!
What I usually do is implementing this two method:
The first one add a UITapGestureRecognizer to the whole UIViewController view
func hideKeyboard() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(tap)
}
The second one just get called every time the user touch anywhere on the UIViewController's view
func dismissKeyboard() {
self.view.resignFirstResponder()
}
I add the first one to the viewDidLoad method of the UIViewController. Or better yet if you want to use that on all the app just make that an extension for your UIViewController.
How about doing this in viewController, It works for me
func dismissKeyboard() {
//All the textFields in the form
let textFields = [textField1, textField2, textField3, textField4, textField5]
let firstResponder = textFields.first(where: {$0.isFirstResponder ?? false })
firstResponder?.resignFirstResponder()
}

Get searchbar text in swift

I want to use search bar in my app. But I couldn't find any tutorial for this.
My question is simple: How can I get search bar text when user preses to enter button ?
I need something like this in my view controller:
override func userPressedToEnter(text: String) {
println("User entered: \(text)")
}
How can I do this in swift ?
Assuming you have a simple search bar in your storyboard, make sure you have it connected as an outlet. Then use this as an example. Use UISearchBarDelegate the reference to learn more about delegate methods available to you.
import UIKit
class ViewController: UIViewController, UISearchBarDelegate {
#IBOutlet var searchBar:UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
searchBar.delegate = self
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
print("searchText \(searchText)")
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
print("searchText \(searchBar.text)")
}
}
I would take a look at the UISearchBarDelegate protocol:
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UISearchBarDelegate_Protocol/index.html
Make your view controller class conform to this protocol and you will have everything you need to interact with your search bar. Alternatively you can get at the search bar text field but Apple gives you a much cleaner, nicer, event driven way via this protocol.
Assuming you have a tableview that you're searching, add a Search Bar and Search Controller to the tableview in the storyboard. That'll hook up all the data source / delegate connections that you need.
Then in your tableview you can use:
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
doStuffWithSearchText(searchBar.text, scope: 0)
}
which will get called whenever they change the text in the search bar. It's common to update the data that's displayed every time they change the text but if you need to do it only when they tap on the search button use this function instead:
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
doStuffWithSearchText(searchBar.text, scope: 0)
}
And you can get the text from the search results controller:
controller.searchBar.text
Or from the search bar:
searchBar.text
If you're not using a tableview controller:
Add a search bar
Hook up your view controller as the search bar's delegate
Then use the searchBarSearchButtonClicked: function to handle when they tap the "Search" button or searchBar(searchBar: UISearchBar, textDidChange searchText: String) to handle w
I wrote a tutorial on doing it with a table view controller that has all the gritty details: Adding a Search Bar to a Table View in Swift

Resources