国家在Swiftui

问题描述 投票:0回答:1
struct BasicView: View { @State var webView: WebView? = nil var body: some View { NavigationView { VStack { WebView(url: "myurl here") { view in print("setView called. Setting webView") self.webView = view } } .navigationTitle("My App") .navigationBarTitleDisplayMode(.inline) .navigationBarItems( leading: Button(action: { guard let view = webView else { print("webView not set") return } view.goBack() }) { Text("Back") } ) } } } }

现在在我的Webview中...

struct WebView: UIViewRepresentable { let url: URL let setParent: (WebView) -> Void var webView: WKWebView init( url: URL, setParent: @ecaping (WebView) -> Void = {_ in }, webView: WKWebView = WKWebView() ) func makeCoordinator() -> Coordinator { Coordinator(self) } func makeUIView(context: Context) -> some WKWebView { webView.navigationDelegate = context.coordinator webView.uiDelegate = context.coordinator webView.load(URLRequest(url: url)) setParent(self) return webView } func updateUIView(_ uiView: UIViewType, context: Context) { } func goBack() { //logic for going back } class Coordinator: NSObject, WKNavigationDelegate, WKUIDelegate { var parent: WebView init(_ parent: WebView) { self.parent = parent } //all my override functions for navigation and whatnot here } }
,现在,当我运行我的应用程序时,我确实会在“ setView nater”。设置webview中看到,然后如果我单击导航栏中的后面按钮,我会看到“ webview not set set”

因此,要调用setparent,应该设置
self.webView
,因为该方法被调用了,但是当我单击导航栏中的按钮时,我会看到

webView not set

显示。因此,当调用SetParent时,状态变量以某种方式不会更新。

这是我第一次看到状态变量未更新并可用。我该如何继续?

	
使用
NavigationLink

以实现您问的东西

...to allow the webview to go "back" meaning the webview loads to a specific URL

swiftui state wkwebview swift-structs
1个回答
0
投票
工作示例测试代码:

import Foundation
import SwiftUI
import WebKit

struct ContentView: View {
    var body: some View {
        BasicView()
    }
}

struct BasicView: View {
    let swiftUIUrl = URL(string: "https://developer.apple.com/xcode/swiftui/")!
    
    let navStackUrl = URL(string: "https://developer.apple.com/documentation/swiftui/navigationstack/")!
    
    var body: some View {
        NavigationStack {
            VStack(spacing: 55) {
                NavigationLink(destination:  WebView(url: swiftUIUrl)) {
                    Text("Go to SwiftUI")
                }
                NavigationLink(destination:  WebView(url: navStackUrl)) {
                    Text("Go to NavigationStack")
                }
            }
            .navigationTitle("My App")
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}

struct WebView: UIViewRepresentable {
    let url: URL

    func makeUIView(context: Context) -> WKWebView {
        let webView = WKWebView()
        return webView
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        let request = URLRequest(url: url)
        uiView.load(request)
    }
}
其他工作示例代码使用数据模型:

// the data model struct MyData: Identifiable { let id = UUID() var url: URL? var name: String } struct BasicView: View { // the data let urls = [ MyData(url: URL(string: "https://developer.apple.com/xcode/swiftui/"), name: "SwiftUI"), MyData(url: URL(string: "https://developer.apple.com/documentation/swiftui/navigationstack/"), name: "NavigationStack") ] var body: some View { NavigationStack { List(urls) { data in NavigationLink(destination: WebView(url: data.url)) { Text("Go to \(data.name)") } } .navigationTitle("My App") .navigationBarTitleDisplayMode(.inline) } } } struct WebView: UIViewRepresentable { let url: URL? func makeUIView(context: Context) -> WKWebView { let webView = WKWebView() return webView } func updateUIView(_ uiView: WKWebView, context: Context) { if let url { let request = URLRequest(url: url) uiView.load(request) } } }

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.