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
Swift: UISearchBar: Get text when search button clicked
提问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 viewDidLoad
you must set the delegate of the search bar to be receiving searchBarSearchButtonClicked
from 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 UISearchBarDelegate
class...
Swift 编码人员容易忘记的两件事: 1. 确保添加UISearchBarDelegate
类...
ViewController: UIViewController, UISearchBarDelegate
Don't forget to identify your view class as the delegate... Inside
viewDidLoad
you could write...searchBar.delegate = self
不要忘记将您的视图类标识为委托......在里面
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.
...这是由委托协议提供给您的。