更改变量后,内容视图不会更新

时间:2020-09-17 18:48:14

标签: ios swift swiftui swiftui-navigationlink

我有一个显示网站的基本应用程序,当我按下按钮时,它应该将我发送到另一个链接,但它并不说明为什么我尝试使用@state bool并在按下按钮但不使用时更改了它 网站已加载,但按钮不会更改网站

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>){
        
    }    
}

2 个答案:

答案 0 :(得分:0)

以下是使用@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))
        }
    }
}

答案 1 :(得分:0)

因为当websi更改时您不会撤回WebView,所以它将无法执行任何操作。您需要在网址中添加@Binding标记,以便它知道要更新。然后你可以打电话

WebView(self.$websi)