I have simple SwiftUi WebView codes and I want to reload WebView when my button clicked (I added only function and simple codes).
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)
}
}
}
}
myWebViewController() 创建一个新的 web 视图实例
You can make this with NotificationCenter, register observer in class where you need to refresh web view, for example in viewDidLoad:
NotificationCenter.default.addObserver(self, selector: #selector(funcUpdateWebView(notification:)), name: NSNotification.Name(rawValue: "updateWebView"), object: nil)
Then setup function for update (also in the class where you need web view to be updated)
@objc func funcUpdateWebView(notification: NSNotification) {
webview.reload()
}
Then post notification from another view controller (instead of myWebViewController().webview.reload()
) like this:
NotificationCenter.default.post(name: NSNotification.Name("updateWebView"), object: nil)
This will do the work.
Also don't forget to deinit this notification in first class outside any functions:
deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "updateWebView"), object: nil)
}
UPDATE:
Another way is to make delegate.
protocol UpdateView {
func update()
}
Subscribe to UpdateView delegate by adding UpdateView to the class myWebViewController.
class myWebViewController: UIViewController, WKNavigationDelegate, UpdateView {
in NewRecordButton create weak var delegate:
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()
In myWebViewController setup delegate for newRecordButton and function that will reload webView.
class myWebViewController: UIViewController, WKNavigationDelegate, UpdateView {
let NewRecordButton = NewRecordButton()
newRecordButton.delegate = self
...
func update() {
webview.reload()
}
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.