ios 快速获取搜索栏文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30058311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:53:57  来源:igfitidea点击:

Get searchbar text in swift

iosswiftuisearchbar

提问by Okan

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 ?

我怎样才能快速做到这一点?

回答by Frankie

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 referenceto learn more about delegate methods available to you.

假设您的故事板中有一个简单的搜索栏,请确保将其连接为插座。然后以此为例。 使用 UISearchBarDelegate 参考来了解有关可用的委托方法的更多信息。

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

}

回答by Daniel Rushton

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

我会看看 UISearchBarDelegate 协议: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 canget at the search bar text field but Apple gives you a much cleaner, nicer, event driven way via this protocol.

使您的视图控制器类符合此协议,您将拥有与搜索栏交互所需的一切。或者,您可以在搜索栏文本字段中找到,但 Apple 通过此协议为您提供了一种更简洁、更好的事件驱动方式。

回答by Christina

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.

假设您有一个正在搜索的 tableview,请将搜索栏和搜索控制器添加到故事板中的 tableview。这将连接您需要的所有数据源/委托连接。

Then in your tableview you can use:

然后在您的 tableview 中,您可以使用:

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:

如果您没有使用 tableview 控制器:

  • 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
  • 添加搜索栏
  • 连接你的视图控制器作为搜索栏的代理
  • 然后使用 searchBarSearchButtonClicked: 函数来处理当他们点击“搜索”按钮或 searchBar(searchBar: UISearchBar, textDidChange searchText: String) 来处理 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

我写了一篇关于使用包含所有细节的表格视图控制器的教程:在 Swift 中将搜索栏添加到表格视图