Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presenting a Sheet modally, @State value is not changing

Tags:

swiftui

I have tried much time on it but I couldn't figure out why it is not working.

The problem is when I tap the button, the new value inside the sheet is not updated. It always show the same value which is set up in start.

 @State var value:String = "empty"
 @State var explorePageIsEnabled:Bool = false
    
   VStack{
    
    Button("tap me"){
        value = "the new one"
        exploreStatusIsEnabled.toggle()
    }
    .sheet(isPresented: $exploreStatusIsEnabled, content: {
        Text(value)
    })
    
}

Deployment target is IOS 14+

like image 555
TuracBey Avatar asked Sep 16 '25 14:09

TuracBey


1 Answers

Create a separate struct view for text and use Binding.

struct SheetView: View {
    @Binding var value: String
    
    var body: some View {
        Text(value)
    }
}

struct ContentView: View {
    
    @State private var value: String = "empty"
    @State private var explorePageIsEnabled: Bool = false
    
    var body: some View {
        VStack{
            
            Button("tap me"){
                value = "the new one"
                explorePageIsEnabled.toggle()
            }
            .sheet(isPresented: $explorePageIsEnabled, content: {
                SheetView(value: $value)
            })
        }
    }
}
like image 84
Raja Kishan Avatar answered Sep 18 '25 09:09

Raja Kishan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!