gpt4 book ai didi

ios - 我的 UI 搜索栏无法工作/向其他网站发送请求

转载 作者:行者123 更新时间:2023-11-30 11:05:56 26 4
gpt4 key购买 nike

我对此还很陌生,但我很困惑为什么每次我输入网址时我的搜索栏都不起作用。我正在尝试制作一个视频下载应用程序。

import UIKit
import AFNetworking
import Alamofire

class ViewController: UIViewController {

@IBOutlet var Webview: UIWebView!
@IBOutlet var SearchBar: UISearchBar!

override func viewDidLoad() {


if let url = URL(string: "https://www.google.com"){

let requestObj = URLRequest(url: url)

Webview.loadRequest(requestObj)
}
SearchBar.text = "http://"

}

func searchBarSearchButtonClicked(searchbar: UISearchBar) {

searchbar.resignFirstResponder()

let text = SearchBar.text
let url = NSURL(string: text!)
let request:URLRequest = URLRequest(url: url! as URL)

//let request = NSURLRequest(URL: url! as URL)

Webview.loadRequest(request)

}

}

最佳答案

添加UISearchBarDelegate协议(protocol)方法来触发搜索栏协议(protocol)​​方法;

class ViewController: UIViewController, UISearchBarDelegate, UIWebViewDelegate {
override func viewDidLoad() {
SearchBar.delegate = self
webview.delegate = self // connect your webview in storyboard
}

// MARK: - UISearchView Delegates
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
}

func searchBarSearchButtonClicked(searchbar: UISearchBar) {
// call your function here
let searchText = searchbar.text!
let url = URL (string: searchText)
let requestObj = URLRequest(url: url!)
webView.loadRequest(requestObj)

}

}

关于ios - 我的 UI 搜索栏无法工作/向其他网站发送请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52729994/

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