我试图让它在一个完成加载后加载 2 个 webview 然后它将转到下一个并开始加载那个,如果两个都已加载,它将发送通知。
之后报错
@IBAction func MybuttonAction(_ sender: Any) {
if !googleLoaded {
googleLoaded = true
let url = URL(string: "https://google.com")
let requestObj = URLRequest(url: url!)
webView.loadRequest(requestObj)
print("Google loaded")
let url1 = URL(string: "https://yahoo.com")
let requestObj1 = URLRequest(url: url1!)
webView.loadRequest(requestObj1)
print("yahoo loaded")
} else if !yahooLoaded {
yahooLoaded = true
let alertController = UIAlertController(title: "ThankYou", message: "Both Views have finished loading", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
self.aviLoadingSpinner.stopAnimating()
}
}
func webViewDidFinishLoad(_ webView: UIWebView) {
}
最佳答案
您必须使用 UIWebViewDelegate webViewDidFinishLoad。当请求完成时调用该方法。
import WebKit
class yourViewController: UIViewController, UIWebViewDelegate {
@IBoutlet weak var webView: UIWebView!
var googleLoaded: Bool = false
var yahooLoaded: Bool = false
override func viewDidLoad() {
self.webView.delegate = self
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if !googleLoaded {
googleLoaded = true
let url = URL(string: "https://yahoo.com")
let requestObj = URLRequest(url: url!)
webView.loadRequest(requestObj)
} else if !yahooLoaded {
yahooLoaded = true
let alertController = UIAlertController(title: "ThankYou", message: "Both Views have finished loading", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
self.aviLoadingSpinner.stopAnimating()
}
}
@IBAction func MybuttonAction(_ sender: Any) {
let url = URL(string: "https://google.com")
let requestObj = URLRequest(url: url!)
webView.loadRequest(requestObj)
}
关于swift - 试图让 webView 加载 2 个 URLS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41872828/