UISearchcontroller searchbar delegate not working
Make delegate of searchbar
in viewdidload
method.
override func viewDidLoad() {
super.viewDidLoad()
searchBarCustom.delegate = self
}
or you can set it in storyboard as
-hence delegates will call as
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("searchBarCancelButtonClicked")
}
EDIT:
For UISearchController
var searchController: UISearchController!
func configureSearchController() {
// Initialize and perform a minimum configuration to the search controller.
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
// Place the search bar view to the tableview headerview.
tblSearchResults.tableHeaderView = searchController.searchBar
}
Why are my UISearchController delegate methods not being called?
You have a property named searchController
. But then you create a local variable of the same name in your configureSearchController
method. So in the end you never set the searchController
property. As a result, your locally created search controller goes out of scope, has no more references, and gets deallocated.
The fix is to update your code a little.
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
searchController.dimsBackgroundDuringPresentation = NO;
searchController.searchBar.placeholder = @"Search for friends...";
searchController.delegate = self;
searchController.searchBar.delegate = self;
[searchController.searchBar sizeToFit];
[searchController.searchBar setBarStyle:UIBarStyleBlack];
[searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
searchController.searchBar.tintColor = [GBColor accentGray];
searchController.searchBar.backgroundColor = [GBColor darkGray];
searchController.searchBar.barTintColor = [GBColor accentGray];
searchController.searchBar.searchTextPositionAdjustment = UIOffsetMake(18.0f, 0.0f);
self.searchController = searchController;
Unable to add UISearchController's search bar into navigationbar and delegate methods not getting called
Just define UISearchController
as property then everything working fine.
I have tested below in the sample project.
class ViewControllerName: UIViewController{
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
let search = UISearchController(searchResultsController: nil)
self.navigationItem.searchController = search
self.navigationItem.searchController!.searchBar.delegate = self
self.navigationItem.searchController!.searchResultsUpdater = self
self.navigationItem.searchController?.searchBar.showsCancelButton = true
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
print("Called")
}
func updateSearchResults(for searchController: UISearchController) {
print("Called")
}
}
Hope it will work for you.
Search bar cancel button delegate not called
The function delegate is not being called because you are missing the definesPresentationContext
:
Determines which parent view controller's view should be presented over for presentations of type
UIModalPresentationCurrentContext. If no ancestor view controller has this flag set, then the presenter
will be the root view controller.
you may enable such flag in this way:
import UIKit
class ViewController: UIViewController, UISearchBarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.searchController = UISearchController(searchResultsController: nil)
self.navigationItem.searchController?.searchBar.delegate = self
self.definesPresentationContext = true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("searchBarCancelButtonClicked")
}
}
Note:
without definesPresentationContext
, you are not really touching the cancel button (when you try to touch it), you are just dismissing the context (you may notice a "silent" glitch in the background focus), suchlike
a popover is being dismissed.
UISeachController Delegate methods not called, searchbar can't become first responder
The problem is you are trying to access UI element(searchbarcontroller) before UI is completely loaded.
This can be done in 2 ways
Use main queue to show keypad
private func setupSearchController() {
self.navigationItem.searchController = searchController
searchController.definesPresentationContext = true
searchController.delegate = self
searchController.isActive = true
searchController.searchBar.delegate = self
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
With this approach keypad will be only show at one time in viewDidLoad
Show keypad in viewDidAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
With this approach keypad will be always shown whenever screen appears.
UISearchController does not trigger delegate methods and search bar delegate methods
searchController.active = YES;
This helped, not sure why it is not mentioned anywhere.
Related Topics
How to Handle Oauth-Style Log Ins with Imessage Apps in iOS 10, Xcode 8
Differrence Between Closure and Function as Argument in Swift
Saving Contact Address to Unified Contact Results in (Cnerrordomain Error 500)
Moya/Alamofire - Url Encoded Params with Same Keys
How to Get User Data from Facebook Sdk on iOS
Transition Delegate for Uitabbarcontroller Animation
How to Update a Swiftui View That Was Embedded into Uikit
How to Determine If a Business Is Open Given the Hours of Operation (Swift-Ios)
Display UI (Storyboard Possibly) with Broadcast UI Extension
Manage a Uipickerview from an External Class - Using Swift
Convert Urlrequest to Nsmutableurlrequest
Wkwebview Blank After 'Successful' Https Nsurlrequest
iOS Swift Detecting Image Rotation
Xcode 12.5 Missing Entitlement Com.Apple.Developer.Associated-Appclip-App-Identifiers
Why Sending Message from Watchkit Extension to iOS and Getting Back a Reply Is So Slow