Gabriele Feudo
Gabriele Feudo

Reputation: 59

Parent View doesn't refresh after child view is closed

I have a simple view (parent view) that has the following NavigationLink:

NavigationLink(
    destination: QuizScreen(quizPack: self.quizPack, isQuizPackOpen: $isQuizPackOpen),
    isActive: $isQuizPackOpen,
    label: {
        Text("Open")
            .font(.custom("Lato-Bold", size: 17))
            .foregroundColor(Color("colorAccent"))
    }
)

And the child view closing itself simply setting the Binding parameter isQuizPackOpen to false.

Using the debugger I noticed that when I close the child view, the parent view does not refreshes like I was changing a simple @State variable.

Is it possible to refresh the parent view after the isQuizPackOpen parameter is set to false by the child view?

Upvotes: 0

Views: 636

Answers (1)

Ouail Bellal
Ouail Bellal

Reputation: 1794

Using onAppear

@State var isQuizPackOpen = false
@ObservableObject viewModel = MyViewModel()
VStack {
   NavigationLink(destination: QuizScreen(quizPack: self.quizPack, isQuizPackOpen:$isQuizPackOpen),isActive: $isQuizPackOpen,
    label: {
            Text("Open")
                .font(.custom("Lato-Bold", size: 17))
                .foregroundColor(Color("colorAccent"))
            })
}.onAppear {
        print("ContentView appeared!")
        if !isQuizPackOpen {
         viewmodel.relaodOrRefreshData()
        }
}

And if you need to refresh it just in some conditions , pass another @State variable to your QuizScreen , and check on it instead of if !isQuizPackOpen

Upvotes: 1

Related Questions