【发布时间】:2022-12-17 07:24:30
【问题描述】:
我有一个带有几个按钮的Menu。每个按钮,代表一个 URL。
选择其中一个按钮后,我想显示一个 webView 使用 .fullScreenCover(isPresented:) 加载所述 URL
@State private var showWebPage = false
@State private var urlToLoad = ""
...
View()
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Menu {
Button("FAQ", action: {
presentWebView(for: "https://example.com/faqsLink")
})
Button("Privacy Policy", action: {
presentWebView(for: "https://example.com/privacyLink")
})
Button("Terms and Conditions", action: {
presentWebView(for: "https://example.com/termsLink")
})
}
}
}
.fullScreenCover(isPresented: $showWebPage) {
WebView(url: URL(string: urlToLoad)!)
}
private func presentWebView(for url: String) {
urlToLoad = url
showWebPage.toggle()
}
每次我尝试这个时,当我切换showWebPage时,urlToLoad仍然是空的
我觉得这与 @State 的工作方式有关,但无法弄清楚,我还是 SwiftUI 的新手。
【问题讨论】:
标签: swift swiftui swiftui-state swiftui-menu