SwiftIOS
SwiftIOS

Reputation: 93

SwiftUI - Dismiss sheet to another view

I am trying to navigate to another View when sheet is dismissed, but not to the original

 @State var showOnboarding: Bool = false
 Button(action: {
                    self.showOnboarding.toggle()
                }) {
                    Text("Click me")

                }.sheet(isPresented: $showOnboarding) {

                    DiscoverView(showOnboarding: self.$showOnboarding)

                }

My question: It is possible to put something like .onDissapear{ NewView() } ?

Upvotes: 1

Views: 956

Answers (1)

pawello2222
pawello2222

Reputation: 54641

If I understood your question correctly you can use onDismiss to perform actions when a sheet is dismissed:

.sheet(isPresented: $showOnboarding, onDismiss: {
    // on dismiss
    // here you can set some variables for presenting another sheet or navigating to some other views
}) {
    DiscoverView(showOnboarding: self.$showOnboarding)
}

You can present another View programmatically using a NavigationLink with an isActive parameter:

NavigationLink(destination: NewView(), isActive: $linkActive) {
    EmptyView()
}

Summing up your code can look like this:

struct ContentView: View {
    @State var showOnboarding: Bool = false
    @State var linkActive: Bool = false

    var body: some View {
        NavigationView {
            VStack {
                Button(action: {
                    self.showOnboarding.toggle()
                }) {
                    Text("Click me")
                }
                NavigationLink(destination: NewView(), isActive: $linkActive) {
                    EmptyView()
                }
            }
        }.sheet(isPresented: $showOnboarding, onDismiss: {
            self.linkActive = true
        }) {
            DiscoverView(showOnboarding: self.$showOnboarding)
        }
    }
}

Upvotes: 1

Related Questions