HomeiOS Developmentios - Methods to replace view created with @ViewBuilder when associated @State...

ios – Methods to replace view created with @ViewBuilder when associated @State modifications?


Right here is the code:

struct DemoModel {
    var textual content: String
}

struct DemoView: View {
    @State var viewModel = DemoViewModel()
    var physique: some View {
        VStack {
            Textual content("Good day, ")
            viewModel.builder?(DemoModel(textual content: "World!"))
            viewModel.builder?(DemoModel(textual content: "World2!"))
            viewModel.builder?(DemoModel(textual content: "World3!"))
        }
    }
    
    func viewBuilder(@ViewBuilder builder: @escaping (DemoModel) -> V) -> Self {
        viewModel.builder = { mannequin in
            AnyView(builder(mannequin))
        }
        return self
    }
}

class DemoViewModel {
    var builder: ((DemoModel) -> AnyView)? = nil
}

struct WrapperDemoView: View {
    @State var worth = "🏆"
    var physique: some View {
        VStack(spacing: 10) {
            HStack {
                Button("ADD 1") {
                    worth += "1"
                }
                Button("ADD 2") {
                    worth += "2"
                }
            }
            Textual content(worth)
            DemoView()
                .viewBuilder { mannequin in
                    HStack {
                        Textual content(mannequin.textual content + " " + worth)
                    }
                }
        }
    }
}

enter image description here

Why when button “Add 1” or “Add 2” is tapped the associated label is up to date, however the views created from @ViewBuilder already not? How can I make them up to date additionally?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments