Home > database >  How to change State variables one after another in SwiftUI?
How to change State variables one after another in SwiftUI?

Time:12-17

I have a Menu with a few buttons. Each button, represents an URL. On selecting one of the buttons I want to present a webView loading the said URL using .fullScreenCover(isPresented:)

 @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()
}

Everytime I try this, urlToLoad is still empty when I toggle showWebPage I feel it has to do with how @State works but can't figure it out, I'm still new to SwiftUI.

CodePudding user response:

To change the state variables one after another in SwiftUI, you can use the onChange modifier on the Binding for the showWebPage variable.

  @State private var showWebPage = false
  @State private var urlToLoad = ""

  var body: some View {
    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)!)
}
.onChange($showWebPage) { value in
  if !value {
    urlToLoad = ""
  }
}
 {
        WebView(url: URL(string: urlToLoad)!)
      }
  }

  private func presentWebView(for url: String) {
    urlToLoad = url
    showWebPage.toggle()
  }

CodePudding user response:

with the hint from Delano I managed to come up with this:

 .fullScreenCover(isPresented: $showWebPage) {
                    WebView(url: URL(string: urlToLoad)!)
                        .withCloseButton(and: "FAQ")
                }
                .onChange(of: urlToLoad) { value in
                    log.info (value)
                    if !value.isEmpty {
                        showWebPage.toggle()
                    }
                }

and in the button action I just update the value of urlToLoad

Hope this helps out somebody, I spent more time on this than I ever thought a Menu needed.

  • Related