我正在编写一个简单的Swift应用-我在底部有两个选项卡,用于调用不同的URL。我无法区分它们,因此两个标签都将加载第一个URL。
import UIKit
import WebKit
// Add WKWebView programmatically
class ViewController2: UIViewController {
var webView : WKWebView
required init(coder aDecoder: NSCoder) {
self.webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 414, height: 736))
super.init(coder: aDecoder)!
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(webView)
webView.translatesAutoresizingMaskIntoConstraints = false
let views = ["webView" : webView]
let h = NSLayoutConstraint.constraints(withVisualFormat: "H:|[webView]|", options: [], metrics: nil , views: views)
let w = NSLayoutConstraint.constraints(withVisualFormat: "V:|[webView]|", options: [], metrics: nil, views: views)
view.addConstraints(h)
view.addConstraints(w)
let url = NSURL(string: "https://bing.com")
let urlRequest = URLRequest(url: url! as URL)
webView.load(urlRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
class ViewController3: UIViewController {
var webView1 : WKWebView
required init(coder aDecoder: NSCoder) {
self.webView1 = WKWebView(frame: CGRect(x: 0, y: 0, width: 414, height: 736))
super.init(coder: aDecoder)!
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(webView1)
webView1.translatesAutoresizingMaskIntoConstraints = false
let views1 = ["webView1" : webView1]
let h = NSLayoutConstraint.constraints(withVisualFormat: "H:|[webView1]|", options: [], metrics: nil , views: views1)
let w = NSLayoutConstraint.constraints(withVisualFormat: "V:|[webView1]|", options: [], metrics: nil, views: views1)
view.addConstraints(h)
view.addConstraints(w)
let url1 = NSURL(string: "https://www.google.com")
let urlRequest = URLRequest(url: url1! as URL)
webView1.load(urlRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我尝试分离url请求,并且我确定这是一个简单的错误,因为我是swift和Xcode的新手!