我的WKWebView的searchBar无法正常工作

时间:2019-12-20 23:55:53

标签: ios swift

我的应用程序中有4个WkWebViews,我希望它们成为单独的浏览器,现在,当我在第一个浏览器中键入链​​接时,如果我去购物网站并添加产品到购物车,它将控制所有这些浏览器将它们添加到所有4种浏览器中的购物车中,是否有任何方法可以使它们成为单独的浏览器?

@IBOutlet weak var webViewA: WKWebView!
    @IBOutlet weak var webViewB: WKWebView!
    @IBOutlet weak var webViewC: WKWebView!
    @IBOutlet weak var webViewD: WKWebView!
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var searchBar2: UISearchBar!
    @IBOutlet weak var searchBar3: UISearchBar!
    @IBOutlet weak var searchBar4: UISearchBar!


    override var prefersStatusBarHidden: Bool {
        return true
    }



    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "https://www.google.com")
        let request = URLRequest(url: url!)

        webViewA.load(request)
        webViewA.navigationDelegate = self

        webViewB.load(request)
        webViewB.navigationDelegate = self

        webViewC.load(request)
        webViewC.navigationDelegate = self

        webViewD.load(request)
        webViewD.navigationDelegate = self

        searchBar.delegate = self
        searchBar2.delegate = self
        searchBar3.delegate = self
        searchBar4.delegate = self
    }


    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

        let urlStr = searchBar.text!
        _ = searchBar2.text!
        _ = searchBar3.text!
        _ = searchBar4.text!

        let url = NSURL(string: urlStr)
        let req = NSURLRequest(url:url! as URL)



        self.webViewA!.load(req as URLRequest)
        self.webViewB!.load(req as URLRequest)
        self.webViewC!.load(req as URLRequest)
        self.webViewD!.load(req as URLRequest)

    }

1 个答案:

答案 0 :(得分:0)

编辑:尝试更改您的第一个searchBar名称,以便您的委托人可以将searchBar及其参数与您在类中声明的参数区分开。 就我而言,我将其更改为searchBar1

 @IBOutlet weak var searchBar1: UISearchBar!

 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

    let urlStr = searchBar.text!
    let url = NSURL(string: urlStr)
    let req = NSURLRequest(url:url! as URL)


  switch searchBar {
    case searchBar1: self.webViewA!.load(req as URLRequest)
    case searchBar2: self.webViewB!.load(req as URLRequest)
    case searchBar3: self.webViewC!.load(req as URLRequest)
    case searchBar4: self.webViewD!.load(req as URLRequest)
    default: break
    }






}