简体   繁体   中英

Content view is not updating when the variable is changed

I have basic app that display a website and when I press a button it should send me to another link but it does not idk why I tried using @state bool and changing it when button is pressed but no use the website loads but the button does not change the website

ContentView.swift

import SwiftUI
import WebKit
import Foundation

struct ContentView: View {
    @State private var selectedSegment = 0
    @State var websi = "172.20.10.3"
    @State private var websites = ["192.168.8.125", "192.168.8.125/Receipts.php","192.168.8.125/myqr.php"]
    @State private var sssa = ["Home","MyRecipts","MyQr"]
    @State var updater: Bool
    var body: some View {
        HStack {
            NavigationView{
                
                VStack{
                    Button(action: {
                        self.websi = "172.20.10.3/myqe.php"
                        self.updater.toggle()
                    }) {
                        Text(/*@START_MENU_TOKEN@*/"Button"/*@END_MENU_TOKEN@*/)
                    } .pickerStyle(SegmentedPickerStyle());
                   
                              
                               
                                 
                                /* Text("Selected value is: \(websites[selectedSegment])").padding()*/
                      
                  Webview(url: "http://\(websi)")
                    
                }
            }
            .padding(.top, -44.0)

        }
       
    }
    
}



struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView( updater: false)
            .padding(.top, -68.0)
    }
}

Webview.swift

import Foundation
import SwiftUI
import WebKit

struct Webview: UIViewRepresentable {
    var url: String
    func makeUIView(context: Context) -> WKWebView {
        guard let url = URL(string: self.url) else{
            return WKWebView()
        }
        let request = URLRequest(url: url)
        let wkWebView = WKWebView()
        wkWebView.load(request)
        return wkWebView
    }
    
    func updateUIView(_ uiView: WKWebView, context:
        UIViewRepresentableContext<Webview>){
        
    }    
}

Here is a possible solution using an @ObservableObject :

struct WebView: UIViewRepresentable {
    @ObservedObject var viewModel: ViewModel

    func makeUIView(context: UIViewRepresentableContext<WebView>) -> WKWebView {
        return WKWebView()
    }

    func updateUIView(_ webView: WKWebView, context: UIViewRepresentableContext<WebView>) {
        if let url = URL(string: viewModel.url) {
            webView.load(URLRequest(url: url))
        }
    }
}

extension WebView {
    class ViewModel: ObservableObject {
        @Published var url: String

        init(url: String) {
            self.url = url
        }
    }
}

struct ContentView: View {
    @State private var currentWebsite = "https://google.com"

    var body: some View {
        VStack {
            Button("Change") {
                self.currentWebsite = "https://apple.com"
            }
            WebView(viewModel: .init(url: currentWebsite))
        }
    }
}

Because you are not recalling the WebView when the websi changes it won't do anything. You need to add a @Binding tag to the url so that it knows to update. Then you can call

WebView(self.$websi)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM