gpt4 book ai didi

ios - 将 URL 值传递给 SecondView Controller WebView (WKWebView)

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:21 25 4
gpt4 key购买 nike

我已经实现了 QR 扫描器代码,在扫描时我已经得到了 URL 字符串,我需要将它传递给第二个 View Controller 到 WKWebView。但每当我尝试传递 URL 字符串时,它就会变成 null 或在单独的 View 中打开。因为它没有在 secondview Controller 中正确打开。İssue:需要将 url 值传递给第二个 Controller webview。第一个 View Controller :包含二维码扫码——获取网址。第二个 View Controller :包含 WebView 以显示 URL 数据。

第一个 Controller 代码:

if let url = URL(string: decodedURL) {
let vc = SecondViewController(nibName: "SecondViewController", bundle: nil)
let url = URL(string: decodedURL)
vc.webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
vc.webView.autoresizingMask = UIView.AutoresizingMask(rawValue: UIView.AutoresizingMask.flexibleWidth.rawValue | UIView.AutoresizingMask.flexibleHeight.rawValue)
vc.webView.configuration.preferences.javaScriptEnabled = true
self.view.addSubview(vc.webView)
vc.webView.load(URLRequest(url: url!))
self.navigationController?.pushViewController(vc, animated: true)
}

第二个 View Controller :

@IBOutlet weak var webView: WKWebView!

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

最佳答案

SecondViewController 中添加以下变量:

var url: URL?

SecondViewControllerviewDidLoad方法中添加如下代码:

override func viewDidLoad() {
super.viewDidLoad()

if let url = self.url {
self.webView.load(URLRequest(url: url))
}
}

FirstController 中,更新代码如下:

if let url = URL(string: decodedURL) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
vc.url = url
self.navigationController?.pushViewController(vc, animated: true)
}

我希望这能解决您的问题。

关于ios - 将 URL 值传递给 SecondView Controller WebView (WKWebView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56556093/

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