0

I'm trying to set value to a @State variable alertText once a button click action is performed. However it isn't working and value in alertText isn't getting set.

I'm new to SwiftUI so I think I'm missing something obvious. I would appreciate any help.

struct ContentView: View {
@State private var showSheet = false
@State private var alertText : String = "Initial Value"

var body: some View {
    NavigationView{
        Button("Show Sheet", action: {
            alertText = "Modified Value"
            showSheet = true
        })
        .sheet(isPresented: $showSheet, content: {
            Text(alertText)
        })
        .navigationTitle("Test View")
    }
}
}

Issue

Jaffer Sheriff
  • 1,444
  • 13
  • 33
  • 1
    Add `[alertText] in` at the beginning of your sheet closure. You also may want to investigate the sheet(item:) form instead of sheet(isPresented:) which commonly runs into this issue. – jnpdx Jun 28 '22 at 04:39
  • @jnpdx Thank you for the comment. I'm able to make it work by wrapping `alertText ` inside an another struct and using that struct in `sheet(item:)`. By the way thank u for ur time. – Jaffer Sheriff Jun 28 '22 at 08:17

0 Answers0