gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:10:11 25 4
gpt4 key购买 nike

我正在尝试使用 Swift 来学习它,但现在我无法从 UISearchBar

获取文本

现在我的代码是这样的:

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

最佳答案

viewDidLoad 中,您必须将搜索栏的委托(delegate)设置为从中接收 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()
}
}

关于ios - Swift:UISearchBar:单击搜索按钮时获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24792798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com