Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SwiftUI View Builder parameter cannot change state

I am trying to create a SwiftUI view where I can pass a view builder. But by using a view builder parameter I cannot change the state. Is this due to the creation in the init method?
How can I achieve my expected behaviour?

struct Card_Previews: PreviewProvider {
    
    @State
    static var bol: Bool = true
    static var previews: some View {
        Label(title: { content })
    }
    
    // Does not change the value
    private static var content: some View {
        Button(bol.description) {
            bol.toggle()
        }
    }
}


struct Label<Title>: View where Title: View {

    var body: some View {
        title
    }

    let title: Title
    public init(@ViewBuilder title: () -> Title) {
        self.title = title()
    }
}

If something is unclear pleas ask 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your problem is unrelated to @ViewBuilder, it’s because bol is a State & not Binding. To solve this you need to bind the 2 properties like so:

struct Label<Title>: View where Title: View {

    @Binding
    var bol: Bool
    
    var body: some View {
        HStack {
            title
            Button(bol.description) {
                // Changes the value
                bol.toggle()
                print(bol.description)
            }
        }
    }
    
    let title: Title
        
    
    public init(bol: Binding<Bool>, @ViewBuilder title: () -> Title) {
        self.title = title()
        self._bol = bol
    }
}

And in Card_Previews replace Label(title: { content }) with Label(Bol: $bol, title: { content }).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading