Muhamméd Muhamméd - 5 months ago 115
Swift Question

searchBarSearchButtonClicked not working Swift

Iam trying to use UISearchBar but it's not working with me

here's my code

class searchViewController: UIViewController , UITableViewDataSource , UISearchBarDelegate{
var searchResults = [String]()
@IBOutlet weak var searchTable: UITableView!
@IBOutlet weak var mySearchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()


}



func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchResults.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = searchTable.dequeueReusableCellWithIdentifier("searchCell", forIndexPath: indexPath) as! UITableViewCell


return cell
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
print("print anything ")
}

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
print(self.mySearchBar.text)
}



}


when i click in the search button in the keyboard using simulator no thing is happen

i tried searchBarTextDidBeginEditing it's not working too

did i do something wrong ?

Answer

Your code is fine, but you need to set mySearchBar.delegate = self or you could do it on the storyboard, using ctrl+drag the search bar to the viewController and choosing delegate.