Thursday, October 29, 2026
HomeiOS Developmentios - Why do not @State parameter adjustments trigger a view replace?

ios – Why do not @State parameter adjustments trigger a view replace?

[ad_1]

I’m making an attempt to comply with the steerage in a WWDC video to make use of a @State struct to configure and current a baby view. I might anticipate the struct to have the ability to current the view, nevertheless the config.present boolean worth doesn’t get up to date when set by the button.

The code under has two buttons, every toggling a special boolean to indicate an overlay. Toggling showWelcome reveals the overlay however toggling config.present does nothing. This appears to be working as supposed, I simply do not perceive why SwiftUI behaves this fashion. Can somebody clarify why it is not functioning like I anticipate, and/or recommend a workaround?

https://developer.apple.com/movies/play/wwdc2020/10040/ @ 5:14

struct InformationOverlayConfig {
    @State var present = false
    @State var title: String?
}

struct InformationOverlay: View {
    
    @Binding var config: InformationOverlayConfig
    
    var physique: some View {

        if config.title != nil {
            Textual content(config.title!)
                .padding()
                .background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 15))
        }
    }
}


struct TestView: View {
    
    @State personal var configWelcome = InformationOverlayConfig(title: "Title is right here")
    @State personal var showWelcome = false
    
    
    var physique: some View {
        VStack {
            Textual content("howdy world")
            Spacer()
            Button("Toggle struct parameter", motion: {
                configWelcome.present.toggle()
            })
            Button("Toggle boolean state", motion: {
                showWelcome.toggle()

            })
        }
            .overlay(
                VStack {
                    InformationOverlay(config: $configWelcome).opacity(configWelcome.present ? 1 : 0)
                    InformationOverlay(config: $configWelcome).opacity(showWelcome ? 1 : 0)
                })
    }

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments