SwiftUI Alert and Sheets in a Dynamic and Easy Way

One of the struggling topics a learner faces with SwiftUI is presenting alerts and sheets. SwiftUI brings a cool reactive syntax, but sometimes it also has some hassles. In this article, we’ll look at a dynamic, easy way to show a SwiftUI alert or sheet.

In Summary

Starting from scratch

[gist id=”9bffb3c0a4694457b5af201eda11e705″ /]

[gist id=”c9f017eb8728aa83b88d582aa2dc6e42″ /]

How we will use containers?

[gist id=”3b83291417c7d66efe42738fae78da12″ /]

The view model stores alert and sheet containers, which gives us the flexibility to show them from the view model. Our view can also show them.

[gist id=”1bf50fe1ddd9dfb8e9dfac3a03f6d087″ /]

Showing our SwiftUI Alert

[gist id=”61e7d4246ba35b0563f2a95d812bb601″ /]

Conclusion