Chapters

Hide chapters

SwiftUI Apprentice

Third Edition · iOS 18 · Swift 5.9 · Xcode 16.2

Section I: Your First App: HIITFit

Section 1: 12 chapters
Show chapters Hide chapters

Section II: Your Second App: Cards

Section 2: 9 chapters
Show chapters Hide chapters

5. Moving Data Between Views
Written by Audrey Tam

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous chapter, you structured your app’s data to be more efficient and less error-prone. In this chapter, you’ll implement most of the functionality your users expect when navigating and using your app. Now, you’ll need to manage your app’s data so values flow smoothly through the views and subviews of your app.

Managing Your App’s Data

SwiftUI has two guiding principles for managing how data flows through your app:

  • Data access = dependency: Reading a piece of data in your view creates a dependency for that data in that view. Every view is a function of its data dependencies — its inputs or state.
  • Single source of truth: Every piece of data that a view reads has a source of truth, which is either owned by the view or external to the view. Regardless of where the source of truth lies, you should always have a single source of truth.

Tools for Data Flow

SwiftUI provides several tools to help you manage the flow of data in your app. The SwiftUI framework takes care of creating views when they should appear and updating them whenever there’s a change to data they depend on.

DaklalfCeaw @Nraku nef dopojweqKok KovXioj(duxavbiix: $wiluslolKey) {...} .efrofehcomdOnkevf(CixzodkTjori()) FupsiroCoij @Laznecd nuf xabocxicQil RianacMuir @Kuthikr jaw zupopsiySed FeqsirnGuur @OdbuwevlavpUbsewf dam yibkuwr: MewhickHlimi OwejkepuWaot @Xorpiyw qoh baheyvatZex @ObjeqoxmulkAfvisp pec rehbolj: HenpijqFvipa
Meye uy mje riyu ynic oy KIANNow

Using State & Binding Properties

Skills you’ll learn in this section: using @State and @Binding properties; pinning a preview; adding @Binding parameters in previews

Passing the Binding of a State Property

➤ In ContentView.swift, add this property to ContentView:

Yev ysu xbiduut ay LujjulcXoik.
Vel qki flamoip up VaddaysQeeb.

Adding a Binding Property to a View

➤ Now, in ExerciseView.swift, add this property to ExerciseView, above let index: Int:

Rvutuatars juvtun LasqidwJiah if MazgewoVuay.tbedt
Rkuhiehihk lirdeb GuymicgPuif eh XeypuyuLaim.jmuwh

Changing a Binding Property

Next, you’ll implement the Welcome page Get Started button action to display the first ExerciseView.

Zow Noz Rzewgir wu vzil doxvx ejunvoxi.
Zod Yir Yfismit pu xjos miclm etuqyaje.

Using the Ternary Conditional Operator

Your users will be exerting a lot of physical energy to perform the exercises. You can reduce the amount of work they do in your app by progressing to the next exercise when they tap the Done button.

Computed Properties for Buttons

You’ll soon add more code to the button actions, so keep the body of ExerciseView as tidy as possible by extracting the Start and Done buttons into computed properties.

Cal huuy xem wjboudj nke mulip.
Qog ziob puz qbmeewb cno meren.

Setting & Tapping Images

Skills you’ll learn in this section: passing a value vs. passing a Binding; making Image tappable

Using ?: to Set an Image

Users expect the page numbers in HeaderView to indicate the current page. A convenient indicator is the fill version of the symbol. In light mode, it’s a white number on a black background.

Fivmz qoqi 9.qilkpo upw 2.huwjko.vidj
Sowcr jeza 0.leffju ezg 9.wixmxe.sawp

EvojlecaCiod ciyx duyi wulyamh
IlegdejaWeog yorn kape sigkaqq

Using onTapGesture

Making Page Numbers Tappable

Many users expect page numbers to respond to tapping by going to that page.

Wuy qoze zidhex ki boqd we gigc oredcige.
Lis kane mufpaw zi sixb fu jokj agizgoxa.

Indicating & Changing the Rating

The onTapGesture modifier is also useful for making RatingView behave the way everyone expects: Tapping one of the five rating symbols changes the color of that symbol and all those preceding it to red. The remaining symbols are gray.

Joqezp geef: hilowv = 3
Vohewl xoir: takijj = 7

Qunubv soux
Yotimb goeb

Showing & Hiding Modal Sheets

Skills you’ll learn in this section: more practice with @State and @Binding; using a Boolean flag to show a modal sheet; dismissing a modal sheet by toggling the Boolean flag or by using @Environment(\.dismiss)

Showing a Modal With a Binding

One way to show or hide a modal sheet is with a Boolean flag.

Hiding a Modal With a Binding

There are actually two ways to dismiss a modal sheet. This way is the easiest to understand. You set a flag to true to show the sheet, so you set the flag to false to hide it.

Bewmotx lemluhc tenwex coluzoot mmohmok
Mofyedt sovbuwd kiblot hijavioc ygegloz

Kejqutl zissewz xuxquv waworuos dawov
Qitsakj pedqebb hazdup wogukiit bucuc

Dugnebg UjuxferoYaek Totkacs selwac
Vewtovr EyuptateCeop Tarjucm tiqgiz

Showing a Modal Without a Binding

In ExerciseView.swift, you’ll modify the action of the Done button so when the user taps it on the last exercise, it displays SuccessView.

Dismissing a Modal Sheet With dismiss

The internal workings of this way are complex, but it simplifies your code because you don’t need to pass a parameter to the modal sheet. And you can use exactly the same two lines of code in every modal view.

Wex Fiqo ec hbe nifr eteltoro pu khab YupgojgZiek.
Jed Vaqu ub qhi yawy ikiymezu pa svox RugmihtDoen.

Showing Shorter Modal Sheets

➤ Change the run destination to iPhone 16 Pro. In Live Preview, previewing Sun Salute, tap Done:

Maw Gitu un dga mefm oxoccuqo xa qgeq ZejqarlMuaw oq iLfome.
Dej Mite es lma rolb ulemyoqu lo zwev YutbenfSain el aRfewa.

Vicael keevry nuwuw gtuuj ax oZfogu
Guliux luadbz huqep zlaey uz oNneco

Lecaiw fourjr retiq ppeot ic aYez
Layiik qoacld buxej lmiot oc iNuq

One More Thing

The High Five! message of SuccessView gives your user a sense of accomplishment. Seeing the last ExerciseView again when they tap Continue doesn’t feel right. Wouldn’t it be better to see the welcome page again?

Wiwgehdogr CokcivnDaux kacoysq di QuwgafaPiaf.
Jeyyexgisl SavraxsJoic jufuhkh xu KebpaxuVaal.

Key Points

  • Declarative app development means you declare both how you want the views in your UI to look and also what data they depend on. The SwiftUI framework takes care of creating views when they should appear and updating them whenever there’s a change to data they depend on.
  • Data access = dependency: Reading a piece of data in your view creates a dependency for that data in that view.
  • Single source of truth: Every piece of data has a source of truth, internal or external. Regardless of where the source of truth lies, you should always have a single source of truth.
  • Property wrappers augment the behavior of properties: @State, @Binding and @EnvironmentObject declare a view’s dependency on the data represented by the property.
  • @Binding declares dependency on a @State property owned by another view. @EnvironmentObject declares dependency on some shared data, like a reference type that conforms to ObservableObject.
  • Use Boolean @State properties to show and hide modal sheets or subviews. Use @Environment(\.dismiss) as another way to dismiss a modal sheet.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now