SwiftUi Button操作重新加载WebView

时间:2019-12-28 20:40:12

标签: swift button swiftui wkwebview

你好,我有简单的SwiftUi WebView代码,我想在单击按钮时重新加载WebView。(我只添加了功能和简单代码。)

class myWebViewController: UIViewController, WKNavigationDelegate {


    var webview: WKWebView

    func reload(){
        webview.reload()
    }

}


   override func viewDidLoad() {
        super.viewDidLoad()


       let overlayView = UIHostingController(rootView: NewRecordButton())
       addChild(overlayView)

       overlayView.view.backgroundColor = UIColor.clear
        overlayView.view.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
       overlayView.view.isUserInteractionEnabled = true


       self.view.addSubview(overlayView.view)
       overlayView.didMove(toParent: self)

    }

    struct NewRecordButton: View {
        @State var color = false
        var body: some View {

                    HStack {
                        Spacer()
                       Button(action: {

                        myWebViewController().webview.reload() // HERE MY ACTION BUT DONT WORK

                                           }, label: {
                                               Text("+")
                                               .font(.system(.largeTitle))
                                               .frame(width: 35, height: 35)
                                               .foregroundColor(Color.white)
                                               .padding(.bottom, 7)


                                           })
                                           .background(Color.blue)
                                           .cornerRadius(38.5)
                                           .padding()
                                           .shadow(color: Color.black.opacity(0.3),
                                                   radius: 3,
                                                   x: 3,
                                                   y: 3)
             }

        }
    }
}

谢谢

2 个答案:

答案 0 :(得分:1)

您可以使用NotificationCenter进行此操作,在需要刷新Web视图的类中注册观察者,例如在viewDidLoad中:

NotificationCenter.default.addObserver(self, selector: #selector(funcUpdateWebView(notification:)), name: NSNotification.Name(rawValue: "updateWebView"), object: nil)

然后设置更新功能(也在需要更新Web视图的类中)

@objc func funcUpdateWebView(notification: NSNotification) {
    webview.reload()
}

然后像这样从另一个视图控制器(而不是myWebViewController().webview.reload())发布通知:

NotificationCenter.default.post(name: NSNotification.Name("updateWebView"), object: nil)

这将完成工作。

也不要忘记在任何函数之外的第一类中取消此通知:

deinit {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "updateWebView"), object: nil)
}

更新:

另一种方法是委托。

protocol UpdateView {
    func update()
}

通过将UpdateView添加到类myWebViewController来订阅UpdateView委托。

class myWebViewController: UIViewController, WKNavigationDelegate, UpdateView {

在NewRecordButton中创建弱var委托:

struct NewRecordButton: View {
    weak var delegate: UpdateView?

    // Replace "myWebViewController().webview.reload()" with this line
    // This will start update() function that will update webview in myWebViewController
    self.delegate.update()

在myWebViewController的newRecordButton和设置代理中,该代理将重新加载webView。

class myWebViewController: UIViewController, WKNavigationDelegate, UpdateView {
    let NewRecordButton = NewRecordButton()
    newRecordButton.delegate = self

    ...
    func update() {
       webview.reload()
    }
    ...
}

答案 1 :(得分:0)

myWebViewController()创建Web视图的新实例