SwiftUI - Pop to root view, after the app has been closed

145 views Asked by At

I have implemented the solution found at the following link to return to my root view:

struct ContentView: View {
@State private var isActive: Bool = false
@AppStorage("setupComplete") var setupComplete = false

var body: some View {
    NavigationView {
        
        if setupComplete == false {
        
        NavigationLink(
            destination: ContentView2(isActive: self.$isActive),
            isActive: self.$isActive) {
            Text("Begin!")
        }

    }
        
        else {
            
            ContentView3(isActive: self.$isActive)
        }
        
    }
}}


struct ContentView2: View {
@Binding var isActive: Bool

var body: some View {
    NavigationLink(
        destination: ContentView3(isActive: self.$isActive)) {
            Text("View Results")
        }

}}

struct ContentView3: View {
@Binding var isActive: Bool

var body: some View {
    
    VStack {
        
        Button(action: {
            self.isActive = false
        }, label: {
            Text("Reset - Perform setup again")
        })
        
    }

}}

This works fine. ContentView + ContentView2 are 'setup' screens, after which ContentView3 is displayed, which displays the results of that setup

I am setting an @Appstorage variable (in a different file) called "setupComplete", to trigger the opening of ContentView3 upon an app launch, if the user has previously performed this setup (so that the user doesnt have to perform the setup each time they open the app)

However,

If I close the app, and re-open (onto ContentView 3), clicking the 'Reset - Perform setup again' button does not return the user to the root view controller (ContentView 1) to perform the setup again

How can I achieve this?

Thanks

0

There are 0 answers