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

How to dismiss the calling screen of a sheet?

Forums > SwiftUI

In SwiftUI, how do we roll back the navigation or dismiss the "parent" or calling screen from a sheet?

I have the common app flow: ListView -> DetailView. From the DetailView I trigger a Sheet for editing. If I delete the item in the Sheet, how can I roll the DetailView back to the ListView? The item for the DetailView is nil because it's deleted, so how can immediately trigger the roll back at the time of the delete event, in the Sheet?

DeletefromSheet

Note, I'm using Core Data as the persistant store; no problem with deletions. I've seen a number of topics on programmatically dismissing screens, navigationView, etc. But, I can't seem to get my head around how to deal with this scenario.

Thanks.

Rajiv

2      

hi Rajiv,

i don't think you can roll-back as you want with some sort of direct call, but i'll offer the cheap alternative: the DetailView has a "Delete This Item" button to do the deletion (which deletes and then dismisses the DetailView).

there's no reason to require moving to an edit screen just to delete an item.

hope that's of interest,

DMG

2      

Thanks, DMG. Do you have any links to learn more about the "Delete This Item" button?

The reason for the Sheet, is to enable other edits, such as changing text. I figured I'd place the delete functionality along side the other edits, rendering the DetailView as purely a read only screen. But, that's not a fixed requirement, so the "Delete This Item" option you mentioned may work on the DetailView.

2      

Rajiv,

with apologies to several reader of this forum who have probably seen this before ... my Shopping List project on Github does exactly this: the "detail view"of an item has a "Delete this Item" button, although the "detail view" is really an edit view on its own -- i felt no need to show everything and then go to another screen to edit anything i saw. i also use this same "detail view" as a sheet to add a new item.

because you're using Core Data, and if you are using a @FetchRequest to drive your list, please don't be surprised if you run into a few problems (depending on what version of XCode/iOS you are using). i'll be curious to see if you have further questions.

hope that helps,

DMG

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.