ios Swift:UISearchBar:单击搜索按钮时获取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24792798/
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 01:25:38  来源:igfitidea点击:

Swift: UISearchBar: Get text when search button clicked

iosswiftcocoa-touchuikituisearchbar

提问by Eudoxus

I'm playing around with Swift to learn it and right now I'm having trouble getting the text from a UISearchBar

我正在玩 Swift 来学习它,现在我无法从 UISearchBar

Right now my code looks like this:

现在我的代码是这样的:

import UIKit

class SecondViewController: UIViewController {

    @IBOutlet var myWebView : UIWebView
    @IBOutlet var adressbar: UISearchBar

    override func viewDidLoad() {
        super.viewDidLoad()

        adressbar.showsScopeBar = true

        var url = NSURL(string: "http://google.com")
        var request = NSURLRequest(URL: url)

        myWebView.scalesPageToFit = true
        myWebView.loadRequest(request)
    }

    func searchBarSearchButtonClicked( searchBar: UISearchBar!) {

        var url = NSURL(string: adressbar.text)
        var request = NSURLRequest(URL: url)

        myWebView.scalesPageToFit = true
        myWebView.loadRequest(request)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

回答by Alex Zielenski

In viewDidLoadyou must set the delegate of the search bar to be receiving searchBarSearchButtonClickedfrom it:

viewDidLoad您必须设置搜索栏的委托来接收searchBarSearchButtonClicked来自它:

import UIKit

class SecondViewController: UIViewController, UISearchBarDelegate
{
    @IBOutlet var myWebView : UIWebView
    @IBOutlet var adressbar: UISearchBar

    override func viewDidLoad()
    {
        super.viewDidLoad()

        adressbar.showsScopeBar = true
        adressbar.delegate = self

        var url = NSURL(string: "http://google.com")
        var request = NSURLRequest(URL: url)

        myWebView.scalesPageToFit = true
        myWebView.loadRequest(request)
    }

    func searchBarSearchButtonClicked( searchBar: UISearchBar!)
    {
        var url = NSURL(string: searchBar.text)
        var request = NSURLRequest(URL: url)

        myWebView.scalesPageToFit = true
        myWebView.loadRequest(request)
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }
}

回答by Shrikar

1.First thing is to conform to the UISearchbarDelegate.

1.首先要符合UISearchbarDelegate。

2.Set the search bar delegate to self.

2.将搜索栏委托设置为self。

You can find more on this here Swift iOS tutorial : Implementing search using UISearchBar and UISearchBarDelegate

你可以在这里找到更多关于这个Swift iOS 教程:使用 UISearchBar 和 UISearchBarDelegate 实现搜索

回答by John Pitts

Simple (non-specific) answer for those arriving here via search:

对于通过搜索到达这里的人的简单(非特定)答案:

If your outlet name for your UISearchBar is 'searchBar'...

如果您的 UISearchBar 的出口名称是“searchBar”...

@IBOutlet weak var searchBar: UISearchBar!

Then the text entered by the user in UISearchBar is called...

然后用户在 UISearchBar 中输入的文本被调用...

searchBar.text

Two common things Swift coders forget: 1. Make sure to add UISearchBarDelegateclass...

Swift 编码人员容易忘记的两件事: 1. 确保添加UISearchBarDelegate类...

ViewController: UIViewController, UISearchBarDelegate
  1. Don't forget to identify your view class as the delegate... Inside viewDidLoadyou could write...

    searchBar.delegate = self
    
  1. 不要忘记将您的视图类标识为委托......在里面viewDidLoad你可以写......

    searchBar.delegate = self
    

If you forget #1 above you won't be able to auto-complete the methods you'll need to implement like...

如果您忘记了上面的#1,您将无法自动完成您需要实现的方法,例如......

func searchBarSearchButtonClicked(_ seachBar: UISearchBar) { your code }

... which is given to you by the delegate protocol.

...这是由委托协议提供给您的。