我正在创建一个Web浏览器。我有一个文本框,用户可以在其中输入他们想去的地方。当用户转到链接内的另一个链接时,我需要弄清楚如何更新文本框。例如,如果用户想要访问YouTube,则会点击视频。我希望使用当前网站更新所需的网站或NSTextField。我红了多个教程说它与委托有关。我不知道如何去做。
import Cocoa
import WebKit
class ViewController: NSViewController {
@IBOutlet weak var webview: WKWebView!
@IBOutlet var desiredWebsite: NSTextField!
@IBOutlet var goBackButton: NSButton!
@IBOutlet var goHomeButton: NSButton!
@IBOutlet var goForwardButton: NSButton!
override func viewDidLoad() {
super.viewDidLoad()
let initialurl = "https://google.com"
let url = URL(string: String(initialurl))
let request = URLRequest(url: url!)
webview.load(request)
desiredWebsite.stringValue = initialurl
}
@IBAction func desiredWebsiteOnTouch(_ sender: Any) {
let gotoweb = desiredWebsite.stringValue
let url = URL(string: String(gotoweb))
let request = URLRequest(url: url!)
webview.load(request)
desiredWebsite.stringValue = gotoweb
}
@IBAction func goHomeOnTouch(_ sender: Any) {
let gotohome = "https://google.com"
let url = URL(string: String(gotohome))
let request = URLRequest(url: url!)
webview.load(request)
desiredWebsite.stringValue = gotohome
}
@IBAction func goBackOnTouch(_ sender: Any) {
let gotoback = ""
let url = URL(string: String(gotoback))
let request = URLRequest(url: url!)
webview.load(request)
desiredWebsite.stringValue = gotoback
}
override var representedObject: Any? {
didSet {
}
}
}