UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to dismiss a sheet and return to last selected TabView?

Forums > SwiftUI

I have an open source app DeTeXt that has two TabViews, and a modal sheet that is presented in each TabView (its the same sheet in both TabViews). The files MainView.swift, CanvasView.Swift, SearchView.swift and AboutView.swift in the the repository link contains all the relevant code.

I'm running into an issue with dismissing the modal sheet - ideally, once dismissed, it should return to the TabView from which the sheet was presented. However, dismissing it currently leads to SwiftUI refreshing the MainView and returning to the default selected TabView, which is CanvasView. How do I keep track of the TabView from which the sheet was presented?

The codebase for the app is currently based on iOS 15, although I had the same issue with iOS 14 (which is on a separate branch on the repo).

2      

Hi, Paul has a nice tutorial on this: https://www.hackingwithswift.com/books/ios-swiftui/creating-tabs-with-tabview-and-tabitem

On dismiss set the selected tab. You can also use a custom enum for your selection.

2      

Do you mean I should pass the selection variable into each TabView, and then into the sheet view that I am calling , after which I set its value properly on dismiss? Should I pass them as binding variables?

2      

Yes, exactly. There is an initializer with .sheet(isPresented: onDismiss: content:)

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.