【问题标题】:Not able to load WebView on button click单击按钮时无法加载 WebView
【发布时间】:2017-08-22 09:55:19
【问题描述】:

当用户单击按钮但没有加载时,我正在尝试在动态创建的 webview 上加载 url。

代码:

import Cocoa
import WebKit

class ViewController :NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

    @IBAction func mybutton(_ sender: Any) {
        let url = URL(string: "https://www.google.com")
        let request = URLRequest(url: url!)
        let webView =  WebView(frame: NSRect(x: 0, y: 0, width: 400, height: 400))
        webView.mainFrame.load(request)
    }

}

【问题讨论】:

    标签: swift macos webview


    【解决方案1】:

    您仍然需要将 WebView 添加为 ViewController 的子视图:

    @IBAction func mybutton(_ sender: Any) {
        let url = URL(string: "https://www.google.com")
        let request = URLRequest(url: url!)
        let webView =  WebView(frame: NSRect(x: 0, y: 0, width: 400, height: 400))
        webView.mainFrame.load(request)
        self.view.addSubview(webView)
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多