Skip to main content

Working with presentations

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

Working with presentations 관련

SwiftUI by Example

Back to Home

Working with presentations | SwiftUI by Example

Working with presentations

Updated for Xcode 15

SwiftUI’s declarative approach to programming means that we don’t create and present alert and action sheets in the same way as we did in UIKit. Instead, we define the conditions in which they should be shown, tell it what they should look like, then leave it to figure the rest out for itself.

This is all accomplished using modifiers, which attach new UI to our view that will be shown when a condition is satisfied. You can attach as many as you want, and they effectively lie in wait watching until their condition becomes true, at which point they show their UI. For example, you might toggle a Boolean inside a button press, which triggers an alert to show.

You can attach presentations to your main view or any of its children – even to the button that adjusts your state so the presentation triggers. It’s a subtle distinction, but it’s important to understand that these presentations aren’t attached to the button because it’s a button – i.e., that doesn’t in any way make the alert be shown because the button was tapped. Instead, we’re attaching it to our view hierarchy so that SwiftUI is aware that it might be shown at any point.

Similar solutions…
Working with containers | SwiftUI by Example

Working with containers
Working with state | SwiftUI by Example

Working with state
Working with forms | SwiftUI by Example

Working with forms
Working with lists | SwiftUI by Example

Working with lists
Presenting an alert | SwiftUI by Example

Presenting an alert

이찬희 (MarkiiimarK)
Never Stop Learning.