【问题标题】:SwiftUi Button action reload WebViewSwiftUi 按钮操作重新加载 WebView
【发布时间】:2019-12-28 20:40:12
【问题描述】:

我有简单的 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)
             }

        }
    }
}

【问题讨论】:

  • 请不要在您的帖子中添加闲聊内容,例如“你好”和“谢谢”。在技​​术写作中最好避免这些。
  • @halfer 谢谢人 :)

标签: swift button swiftui wkwebview


【解决方案1】:

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

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

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

@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()
    }
    ...
}

【讨论】:

  • with Notification Center 工作,但我不想使用 Notification Center 。 else 有可能吗?
  • 您可以制作委托协议,与 NotificationCenter 的工作方式相同
  • 你可以在同一个答案中显示示例代码吗?我会批准你的回答
  • @SwiftDeveloper 我已经用委托方法更新了答案。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 2020-05-15
    相关资源
    最近更新 更多