How to resignfirstResponser on x button action event in uisearch bar? - ios

I am unable to detect x button action in UISearchBar iOS 9. is there anyone can help me?

if your view controller is your search bar delegate, you could implement searchBarCancelButtonClicked function and resignFirstResponder from there.
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchBar.text = ""
searchBar.resignFirstResponder()
// or you could force view to end editing mode using self.view.endEditing(true)
}

you can do something like,
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.characters.count == 0 {
performSelector("hideKeyboardWithSearchBar:", withObject:searchBar, afterDelay:0)
}
}
refer apple documentation for more details
Hope this will help :)
func hideKeyboardWithSearchBar(bar:UISearchBar) {
bar.resignFirstResponder()
}

Related

Why searchBarTextDidEndEditing not called?

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

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

Detect if "delete" button on iOS keyboard is pressed in empty UISearchBar

I am wondering if there is a way to detect when user press delete button or try to erase character in UISearchBar? I searched for quite a bit on Google and I tried couple of solutions where one put a whiteSpaceCharacter in the empty UITextField and the other use shouldChangeTextInRange delegate to do.
The one with empty space, the user can somehow know that there is a space there by just press hold and highlight the space so I think that was not a very good solution.
The one that use shouldChangeTextInRange does not work when the search bar is empty.
Another thing to note is that majority of the solutions I found was in Objective C and it would be great if there are some way I can do this in Swift. Or would be great if anyone can point me to the right path.
Don't know why you would wan't do delete when the searchBar is empty, but you can do it like this:
import UIKit
class ViewController: UIViewController, UISearchBarDelegate{
private var search : UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
search = UISearchBar()
search.frame = CGRectMake(0, 0, 200, 200)
search.delegate = self
self.view.addSubview(search)
}
func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
let char = text.cStringUsingEncoding(NSUTF8StringEncoding)!
let isBackSpace = strcmp(char, "\\b")
if (isBackSpace == -92) {
print("Backspace was pressed")
}
return true
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if searchText == "" {
search.text = " "
}
}
}
This takes use of the UISearchBarDelegate, and finds out if backspace is pressed, via the shouldChangeTextInRange function. If the string is empty, it reapplies a whitespace to make it continuously deletable. It's kinda tacky, but the only way I know of to do it, as it doesen't register when the field is empty otherwise.
Hope it's of any use.

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];

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