Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when search bar text changes [duplicate]

I have a search bar and I need to get a notification when the bar text changes, how can I do that? I need to reload the table view every time the string value of the search bar changes

like image 951
Eltin Avatar asked Feb 14 '14 19:02

Eltin


1 Answers

Follow the below steps to get it done

Add Delegate methods for your View Controller

ViewController: UISearchBarDelegate, UISearchDisplayDelegate

Create IBOutlet for your searchBar

@IBOutlet weak var searchBarVar: UISearchBar!

Assign Delegates as self to searchBar in viewDidLoad.

override func viewDidLoad() {
    super.viewDidLoad()
    
    searchBarVar.delegate = self
}

Implement the textDidChange delegate method

Swift 4:

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    print("searchText",searchText)
}

ObjC

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;

like image 98
Robert Avatar answered Oct 20 '22 06:10

Robert