this is my code:
extension MyViewController: UISearchBarDelegate {
// MARK: - UISearchBar Delegate
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
}
// MARK: - Cancel
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
myFunc()
print("Cancel")
searchBar.resignFirstResponder()
}
}
all is done as expected when the cancel is clicked, and the Xcode log shows the print "Cancel", but the function "myFunc" is not performed.
any idea to fix this? thanks!
Edit: (for more context)
I have a button that I hide when search bar is clicked with:
func filterContentForSearchText(_ searchText: String, scope: String = "xyz") {
self.exampleButton.alpha = 0.0
....
...
the function "myFunc" is as follows:
func myFunc(){
self.exampleButton.alpha = 1.0
}
I tried also to right what I wanted like this:
// MARK: - Cancel
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
self.exampleButton.alpha = 1.0
print("Cancel")
searchBar.resignFirstResponder()
}
but also didn't work.
my aim is to make the exampleButton appear back when the search is dismissed.
Okay so I found what was the problem.
instead of hiding the button in:
func filterContentForSearchText(_ searchText: String, scope: String = "xyz")
for it to be hidden when the search bar is clicked, I should use it in:
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar)
and now when I make it appear back in the search dismissal, in:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
it works perfectly!