goForward 和 goBack 在 swiftui 中不起作用

goForward and goBack does not work in swiftui

我正在尝试在 swiftui 中添加后退和前进按钮,但这些按钮不起作用。 URL 来自 firebase。 我关注了这个问题。如果有人帮助我,我非常感谢你。 这是源代码

import SwiftUI
import WebKit
import Firebase

struct WebView: View {

    @State var websiteURL: String
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>


    var body: some View {
        ZStack{
            Color.init(red: 172/255, green: 198/255, blue: 224/255).edgesIgnoringSafeArea(.all)
            VStack{
                Button(action: {
                    self.presentationMode.wrappedValue.dismiss()
                }) {
                    Image("back").renderingMode(.original).frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 60, alignment: .leading).padding()
                }

                Webview(web: nil, req: URLRequest(url: URL(string: websiteURL)!))
                HStack{
                    Button(action: {
                        //go to back
                        Webview(web: nil, req: URLRequest(url: URL(string: websiteURL)!)).goBack()
                    }) {

                        Image(systemName: "arrow.left").foregroundColor(.black)
                    }
                    Spacer()
                    Button(action: {
                       // go forwared
                        Webview(web: nil, req: URLRequest(url: URL(string: websiteURL)!)).goForward()
                    }) {

                        Image(systemName: "arrow.right").foregroundColor(.black)
                    }
                }.padding([.leading,.trailing],20)
            }.navigationBarTitle("")
           .navigationBarHidden(true)
        }.onAppear{
            self.loadURL()

        }

    }
    func loadURL(){
           //For Admin
           let rootRef = Database.database().reference().child("Admin").child(websiteURL)

           rootRef.observeSingleEvent(of: .value, with: { (snapshot) in
             // Get user value
             let value = snapshot.value as? NSDictionary

               let getURL:String = value?["value"] as? String ?? ""
            self.websiteURL=getURL


             // ...
             }) { (error) in
               print(error.localizedDescription)
           }
       }
}

struct Webview : UIViewRepresentable {


  let request: URLRequest
  var webview: WKWebView?

  init(web: WKWebView?, req: URLRequest) {
      self.webview = WKWebView()
      self.request = req
  }

  func makeUIView(context: Context) -> WKWebView  {
      return webview!
  }

  func updateUIView(_ uiView: WKWebView, context: Context) {
      uiView.load(request)
  }

  func goBack(){
      webview?.goBack()
  }

  func goForward(){
      webview?.goForward()
  }
}

struct WebView_Previews: PreviewProvider {
    static var previews: some View {
        WebView(websiteURL: "")
    }
}

我支持我的评论,你创建了 3 个不同的 Webview。这是使其工作的一种方法。 复制并粘贴此代码,让我知道它是否适合您。

import SwiftUI
import WebKit
import Firebase


struct WebView: View {

@State var websiteURL: String

@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

let websiteView = Webview()

var body: some View {
    ZStack{
        Color.init(red: 172/255, green: 198/255, blue: 224/255).edgesIgnoringSafeArea(.all)
        VStack{
            Button(action: {
                self.presentationMode.wrappedValue.dismiss()
            }) {
                Image("back").renderingMode(.original).frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 60, alignment: .leading).padding()
            }
            self.websiteView
            HStack{
                Button(action: {
                    // go to back
                    self.websiteView.webview.goBack()
                }) {
                    Image(systemName: "arrow.left").foregroundColor(.black)
                }
                Spacer()
                Button(action: {
                    // go forwared
                    self.websiteView.webview.goForward()
                }) {
                    Image(systemName: "arrow.right").foregroundColor(.black)
                }
            }.padding([.leading,.trailing],20)
        }.navigationBarTitle("")
            .navigationBarHidden(true)
    }.onAppear{
        self.loadURL()
    }
}

func loadURL() {
       //For Admin
       let rootRef = Database.database().reference().child("Admin").child(websiteURL)

       rootRef.observeSingleEvent(of: .value, with: { (snapshot) in
         // Get user value
         let value = snapshot.value as? NSDictionary

        let getURL:String = value?["value"] as? String ?? ""
        self.websiteURL = getURL

        // new code <-----
        self.websiteView.loadRequest(request: URLRequest(url: URL(string: self.websiteURL)!))  

         // ...
         }) { (error) in
           print(error.localizedDescription)
       }
   }    
}


struct Webview : UIViewRepresentable {

@State var request: URLRequest?

let webview = WKWebView()

func makeUIView(context: Context) -> WKWebView  {
    return webview
}

func updateUIView(_ uiView: WKWebView, context: Context) {
    if request != nil { uiView.load(request!) }
}

func loadRequest(request: URLRequest) {
    self.request = request
    webview.load(request)
}

func goBack(){
    webview.goBack()
}

func goForward(){
    webview.goForward()
}
}