Trouble with UISearchBar implementation in swift? - ios

So I am messing around with user queries using Parse. I have a table view as my data source within a UIViewController, and I am using an extension on it like so
extension addFriend: UISearchBarDelegate, UISearchDisplayDelegate {
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
print("worked")
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
print("editing")
searchBar.setShowsCancelButton(true, animated: true)
state = .SearchState
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
searchBar.text = ""
searchBar.setShowsCancelButton(false, animated: true)
state = .DefaultState
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
Reachability.searchUsers(searchText, block: updateList)
}
}
specifically for my search bar declared as
#IBOutlet weak var searchBar: UISearchBar!
Reachability is a public class with my query functions e.g. searchUsers().
I am using a model described here
https://www.makeschool.com/tutorials/build-a-photo-sharing-app-part-2/friend-search-follow
but for some reason the search bar is not working. The print statements are there to verify this fact, and I have tried adopting the search bar delegates at class declaration and typing search bar functions within the class as I had written similar code not long before I tried this. It is not working and I can't exactly figure out why. I have done exactly this before and it worked just fine. I am new to swift programming, is there something I am missing? I would appreciate any help or shove in the right direction. Thanks in advance!

Related

how can i filter array in swift 5 for search text in searchbar?

I am going to search from tableview and for that i used array.filter(), but i can not get any suggestion related to function's parameter, i got "NSArray.Element self", how can add my required parameter to function. i am not able to get a any function like "prefix" or "replaceString" in parameter, so i how can solve this issue?
here is my code,
extension ViewController: UISearchBarDelegate
{
func searchBar(_ searchBar: UISearchBar, textDidChange searchText:String)
{
searchCoin = ArrData.filter ({ $0.prefix(searchText.count) == searchText})
tableView.reloadData()
}
}
To check if any string element in the array contains the search text. Be it prefix, suffix or in between the string
extension ViewController: UISearchBarDelegate
{
func searchBar(_ searchBar: UISearchBar, textDidChange searchText:String)
{
searchCoin = ArrData.filter({ $0.contains(find: searchText)})
tableView.reloadData()
}
}
OR
To check if the any string element in the array has prefix equal to search text
extension ViewController: UISearchBarDelegate
{
func searchBar(_ searchBar: UISearchBar, textDidChange searchText:String)
{
searchCoin = ArrData.filter({ $0.hasPrefix(searchText)})
tableView.reloadData()
}
}
Swati is correct. Swift 5 has slightly different syntax but it's basically this. searchCoin is a new array filter of the original data array that you are searching.
let searchCoin = dataArray.filter({ $0.contains(searchText!)})
I solved my problem using:
let searchCoin = dataArray.filter({$0.lowercased().contains(searchText.lowercased())})

searchBarSearchButtonClicked not working

I have set the delegate in the viewDidLoad() method and also checked the IB to see if the delegate shows up when using (control + drag) and it does infact show as being hooked up. I deleted it as well and added a new SearchBar entirely in IB and hooked it back up. Nothing seems to do the trick. Any suggestions?
override func viewDidLoad() {
self.searchBar.layer.zPosition = 1
self.searchBar.delegate = self
}
//this is never being called, breakpoint never hits
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
print("searchText \(searchText)")
}
//this is never being called, breakpoint never hits
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
print("search button clicked")
self.firebaseQuery()
}
Is your custom View Controller class set in Interface Builder? I.e. on the Identity Inspector tab, ensure your desired UIViewController subclass is set in the Custom Class section.
Also, try setting a breakpoint in viewDidLoad(). Run the app and if the breakpoint doesn't get hit when you expect, that helps narrow down the problem.
Is it possible you're setting the delegate incorrectly?
In Interface Builder, command-click the search bar, drag the delegate to the yellow ViewController button in the storyboard to set the delegate.
Remove self.searchBar.delegate = self from viewDidLoad()
I'm also not seeing if your viewController Class has properly conformed to UISearchBarDelegate I would add it in an extension to your controller and put all your code relating to it there.
class ViewController: UIViewController{...}
extension ViewController: UISearchBarDelegate {
// implement all searchBar related methods here:
func searchBarButtonClicked(_ searchBar: UISearchBar) {
//capture the string
if let input = searchBar.text {
// do something with string.
print(input)
}
}
I also had a problem with it not responding when I set it up using IB. I finally set it up using code instead of IB, and it worked. Try it this way:
class MySearchTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.searchController.searchBar.autocapitalizationType = UITextAutocapitalizationType.none
self.navigationItem.searchController = self.searchController
//if this is set to true, the search bar hides when you scroll.
self.navigationItem.hidesSearchBarWhenScrolling = false
//this is so I'm told of changes to what's typed
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.delegate = self
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
//hopefully this gets called when you click Search button
}
func updateSearchResults(for searchController: UISearchController) {
//if you want to start searching on each keystroke, you implement this method. I'm going to wait until they click Search.
}
https://www.reddit.com/r/swift/comments/85o75h/bug_uisearchbar_delegate_methods_not_being_called/dvzcbb4/
Thanks to reddit user #applishish I got the issue
I did not put a underscore in front of the parameter -_-
Thanks all for your help!

UISearchcontroller Cancel button not working

class PlaceSelectorViewController: UIViewController, GMSAutocompleteResultsViewControllerDelegate, UISearchBarDelegate, UISearchControllerDelegate {
I have UISearchBarDelegate in my class
searchController?.searchBar.delegate = self
and I had set the delegate to Self.
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
stopHighlight()
}
But this Delegate call is not working
You need an outleet for your UISearchBar (assuming you're working with Storyboard this is really easy, just drag and drop via Assistant Editor). and that's what you're going to add the delegate to. In case you built programmatically, then that variable is the one that's going to get assigned to the delegate.
Here's the example code for the Storyboard built
#IBOutlet weak var searchBar: UISearchBar! (you must see a filled circle at the right of this, otherwise there's no link between Storyboard and this outlet)
...
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
}

Swift: How use delegate

I have AllOrganizacionUIView class, I used UISearchBarDelegate delegate, on workink
class AllOrganizacionUIView: UIView , UISearchBarDelegate {
#IBOutlet var SearchBar: UISearchBar!
var searchActive : Bool = false
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchActive = true;
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchActive = false;
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchActive = false;
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchActive = false;
aa.SearchBar.endEditing(true)
print("hajox")
}
}
I Have use UISearchBarDelegate delegate in ViewController class,
class ViewController: UIViewController{
#IBOutlet var SearchBar: UISearchBar!
// not working
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
self.CircleImageView_3.image = UIImage(named: "FullCircle")
self.CircleImageView_2.image = UIImage(named: "Circle")
self.CircleImageView_1.image = UIImage(named: "Circle")
self.mMainCategory.hidden = true
self.mAllCategores.hidden = true
self.mAllOrganizations.hidden = false
self.FooterViewController.text = "Основные категории"
return true
}
}
Help me please with this problem.
You have to set delegate for your search bar, possibly in viewDidLoad():
override func viewDidLoad() {
super.viewDidLoad()
self.SearchBar.delegate = self
}
But it will be much better if you point the delegate on storyboard instead of code.
So, I see two problems in your code. First, you mean that your delegate is an UIView subclass. But then you mean that you want to use delegate which is implemented in UIViewController subclass.
You're able to have only one delegate at the same time. Choose one (view controller is preferred), and implement all delegate methods there. Then just set delegate as I said at the beginning of my answer.
Actually, I suggest you to read about protocols and delegates at the official Apple's documentation website. It is really necessary to read documentation carefully, because there are a lot of unique things in iOS that are different to another platforms.
override func viewDidLoad() {
super.viewDidLoad()
self.SearchBar.delegate = self
}
or set your delegate in storyboard .

searchbarsearchbuttonclicked not called in UICollectionViewController

I have a class and use UICollectionViewController, UICollectionViewDelegateFlowLayout , UISearchBarDelegate
I want use searchbar in Navigation View , but when I clicked on Cancel Button or SearchButton , searchBarSearchButtonClicked and searchBarCancelButtonClicked , non of them was called.
but I use this function and this func was called :
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
println("search is working ")
}
this is my viewDidload func:
override func viewDidLoad() {
super.viewDidLoad()
let searchBar = UISearchBar()
searchBar.delegate = self
searchBar.sizeToFit()
navigationItem.titleView = searchBar
}
and this is search and cancel func:
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
println("cancel button called")
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
println("search button called")
}
So , what is wrong?
searchBar func is called when I write something in searchbar but
when click on searchButton and cancel button nothing happen!?
I use simulator iphone ,So search button is enter button of keyboard
but I used another button , then nothing happen.
now I use enter , and every thing is ok.
this is very funny!:D

Resources