For some reason the @State variable that I am using to update the url of my WebView is not updating. Both buttons result in the 'www.target.com' url.
import SwiftUI
struct ContentView: View {
var siteArray = ["http://www.google.com","http://www.yahoo.com","http://www.amazon.com"]
@State private var siteUrl = "http://www.target.com"
@State private var showWebView = false
var body: some View {
ScrollView{
Text("Websites")
Button("Target") {
showWebView.toggle()
}
Button("Google") {
siteUrl = siteArray[0]
showWebView.toggle()
}
}
.sheet(isPresented: $showWebView) {
WebView(url: URL(string: "\(siteUrl)")!)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Any help would be appreciated. Thanks!
try to use @Binding for exemple:
struct WebView: View {
@Binding var siteUrl :String
var body: some View {
Text(siteUrl)
}
}
struct ContentView: View {
var siteArray = ["http://www.google.com","http://www.yahoo.com","http://www.amazon.com"]
@State private var siteUrl = "http://www.target.com"
@State private var showWebView = false
var body: some View {
ScrollView{
Text("Websites")
Button("Target") {
showWebView.toggle()
}
Button("Google") {
siteUrl = siteArray[1]
showWebView.toggle()
}
}
.sheet(isPresented: $showWebView) {
WebView(siteUrl: $siteUrl)
}
}
}