简体   繁体   中英

Get searchbar text in swift

I want to use search bar in my app. But I couldn't find any tutorial for this.

My question is simple: How can I get search bar text when user preses to enter button ?

I need something like this in my view controller:

override func userPressedToEnter(text: String) {
     println("User entered: \(text)")
}

How can I do this in swift ?

Assuming you have a simple search bar in your storyboard, make sure you have it connected as an outlet. Then use this as an example. Use UISearchBarDelegate the reference to learn more about delegate methods available to you.

import UIKit

class ViewController: UIViewController, UISearchBarDelegate {

@IBOutlet var searchBar:UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        searchBar.delegate = self
    }

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

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        print("searchText \(searchBar.text)")
    }

}

I would take a look at the UISearchBarDelegate protocol: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UISearchBarDelegate_Protocol/index.html

Make your view controller class conform to this protocol and you will have everything you need to interact with your search bar. Alternatively you can get at the search bar text field but Apple gives you a much cleaner, nicer, event driven way via this protocol.

Assuming you have a tableview that you're searching, add a Search Bar and Search Controller to the tableview in the storyboard. That'll hook up all the data source / delegate connections that you need.

Then in your tableview you can use:

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
  doStuffWithSearchText(searchBar.text, scope: 0)
}

which will get called whenever they change the text in the search bar. It's common to update the data that's displayed every time they change the text but if you need to do it only when they tap on the search button use this function instead:

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
  doStuffWithSearchText(searchBar.text, scope: 0)
}

And you can get the text from the search results controller:

controller.searchBar.text

Or from the search bar:

searchBar.text

If you're not using a tableview controller:

  • Add a search bar
  • Hook up your view controller as the search bar's delegate
  • Then use the searchBarSearchButtonClicked: function to handle when they tap the "Search" button or searchBar(searchBar: UISearchBar, textDidChange searchText: String) to handle w

I wrote a tutorial on doing it with a table view controller that has all the gritty details: Adding a Search Bar to a Table View in Swift

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM