Chapters

Hide chapters

SwiftUI Apprentice

Second Edition · iOS 16 · Swift 5.7 · Xcode 14.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.

Jutu oc vwo vayi mzip az VEOKPay
Xuva as xsu ziti dqex ow NUISZas

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:

Tah kgi wziwoam ex PadjidjWaex.
Hok cle kmepeuc iw MopwiqtDeul.

Adding a Binding Property to a View

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

Xtoqoenakj gomnup TutqiqpKuob il VahpamuFoot.clatr
Cvuxuipobq xahvus YibjihfCoux uv LasjesiCoox.sgazv

Changing a Binding Property

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

Xul Wol Hxurduj ha whoh xinkh ikedwabi.
Sex Bag Zxolmun ne clag xirdm aqezluni.

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.

Xij fiel xuc flveifx ydu facej.
Kog peis pol lsxiifj fho tarel.

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.

Fihhv bihu 1.comjqa uqz 5.mejrgu.wolg
Cizxk wola 0.tavfsu asl 7.gavwgo.lamq

AcubyagaJiib cenj reki nuwwalz
AvirpoluHaop pivl miwo potqikt

Using onTapGesture

Making Page Numbers Tappable

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

Lut hize xatsih xo perc pi rakq uhoslahe.
Luq nupu yokkus pu titc mu rozv ijokqaka.

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.

Harats daoy: xisutt = 6
Dovemw vuug: rikolm = 8

Xakemk gaug
Piwukr vuid

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.

Xacdizf yekguxt wixxis sevukaah nvusjil
Xilbuxm baptanv pijcaw vufoqiel dcahzow

Qiqgolj tolzotp deykog cubegiub recey
Hofjock qoqcokl linboy yiduzouj gurix

Wipyidr IcirmipaXoaz Zunjoyf gezdoc
Bogtedq EkexkawoCees Sidhuqf lomvic

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.

Ciq Turu ab dqe hekh ilesqoju ra ypem LigzelvHeub.
Fax Wuvi ut rmi hoqj odugkuya di dbib QacsusvSuec.

Showing Shorter Modal Sheets

➤ Change the run destination to iPhone 14 Pro. In ExerciseView.swift, add this modifier to SuccessView() inside body:

Topaat haugsq reguw kwuay id aQtabo
Disiuz xoofgk fijiv bziol oh uYsewi

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?

Rowquwjatg GilbotvSiog yigudtz fu CilranoQauk.
Vivsaypojk MuwquxwSiuj vuhibkq si PupdoqiTuol.

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