Make Cancel button in search bar active after search button tapped
<p>I have search bar and cancel button, after search button tapped, it display the result but cancel button is inactive and i need to click twice to make it work and back to the initial result. How to make cancel button active after the search result appear?</p>

<p>here's my code</p>

<pre><code>func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    searchBar.endEditing(true)
    searchBar.setShowsCancelButton(true, animated: true)
    filterContentForSearchText(searchBar.text!, scope: "All")
    print(searchBar.text)
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    searchBar.text = ""
    searchBar.resignFirstResponder()
    searchBar.setShowsCancelButton(false, animated: true)
    database = dataUtuh
    self.collectionView.reloadData()
}
</code></pre>

<p>I already implement <code>UISearchBarDelegate</code> and <code>searchBar.delegate = self</code> on my viewDidLoad</p>