Avoid crashes with multiple sheets in Swift UI.

Ravi Bastola
3 min readOct 22, 2021

Swift UI provides powerful tools for iOS developers; be it grids, lists or sheets, scroll views and many more. These tools can be very useful and frustrating for a developer if the implementation is not understood well.

Presenting a sheet inside an iOS app with SwiftUI is simple. A .sheet modifier does the work for you. Let’s look at it with an example:

We added a button inside a view which toggles the .showSheet property to present a sheet. This gives us a modal view as shown below.

Sheet Example

This is a simple example. Now the problems start to arise when you want to show multiple sheet inside your application. You will start to notice that isPresented modifier does not work well while presenting multiple sheets specially in a loop. Lets look into an example:

Here we are presenting multiple sheets within a navigation destination view, which is inside a loop.The destination view which also has multiple buttons which present a respective sheet when tapped. This produces a result shown below:

Multiple Sheets

Wow! this again works. You can create multiple sheets from multiple navigation links and this also amazes you that from the identifable initilizaer allows you to pass data inside your sheet and your code is also clean, with just one initializer of sheet. Neat!

I purposefully added the NavgiationLink to show you another side of the problem with this implementation. As you can see, you might want to go back from the home page where there are multiple navigation links. You know the trick, you just add @Enviroment (.\presentationMode) var presentationMode for iOS 14 and @Environment (\.dismiss) var dismiss for iOS 15 to go back from the page where there are buttons. Let’s look into the code.

which results in:

Navigation back with cross button.

You are good to go, but you notice a strange behavior when you add this navigation behavior. You want to view your sheets and the app crashes.

The sheet opens twice and crashes and you don’t know why this happens. Removing the @Environment does fix the issue but it does not allow you to navigate back to the previous page. You are in a row and start blaming SwiftUI that it is not documented well.

Is it the problem of @Environment ? Let’s dive deep into the code. The root cause of this problem is the getter of the UUID which is intialized many times and causes the crash. Now this presents us with the problem that what should Iwe use to handle cases for the sheet with identifiable. Voila, Let’s create a Struct and conform it to Identifiable and store the UUID as stored property but not computed. Let’s revisit the code again.

Here, I added a struct instead of an enumeration which did not allow us to use the id (UUID) as a stored property.

I also left the enum example to be more clear. Here we see that the struct data structure is used and the id is a stored property. Lets build and run now.

Non-Crashing Sheets

Now the sheets dont crash and you are done with your day. Thank you for reading. Hope this solves your issues related to showing multiple sheets.

--

--