Get searchbar text in swift

China☆狼群 提交于 2020-01-21 04:13:04

问题


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 ?


回答1:


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)")
    }

}



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/30058311/get-searchbar-text-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!