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

22. Lists & Navigation
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

Most apps have at least one view that displays a collection of similar items in a table or grid. When there are too many items to fit on one screen, the user can view more items by scrolling — vertically, horizontally or both. In many cases, tapping an item navigates to a view that presents more detail about the item.

In this section, you’ll start implementing TheMet, an app that searches The Metropolitan Museum of Art, New York for objects matching the user’s query term.

In this chapter, you’ll create a prototype of TheMet with a List of objects in a NavigationStack. Tapping a list item pushes a detail view onto the navigation stack. The starter project already contains ObjectView.swift, which displays some of the object properties.

Getting Started

➤ Open the TheMet app in the starter folder. For this chapter, the starter project initializes the Object data in Preview Content. In Chapter 24, “Downloading Data”, you’ll fetch this data from collectionapi.metmuseum.org.

List

You encountered the SwiftUI List view in Chapter 10, “Working With Datasets”, where you learned how to let users edit the history of exercises in HIITFit.

Leqt wexg wayexoqkawg mopa
Zudd guwk bihumihneyk foga

NavigationStack

In Chapter 13, “Outlining a Photo Collage App”, you used NavigationStack so you could add toolbar buttons to SingleCardView. Navigation toolbars are useful for putting titles and buttons where users expect to see them. But the main purpose of NavigationStack is to manage a navigation stack in your app’s navigation hierarchy. In this section, you’ll push an ObjectView onto the navigation stack when the user taps a List item.

Tuyimaguuv havqa pirialpf gu fixle vaxki.
Xuxofegaic mujqe tolaemhd ji cajlo keqji.

Navigating to a Detail View

Now, you’ll navigate to the object’s ObjectView when the user taps the list item.

Gebivageed zowj po ArjodwBuav
Kunafakoep rudf fe IffeyfRoul

Using the Internet

You’ll soon learn how to download data from an internet server into your app, but first you’ll see a couple of ways to use the device’s default browser.

Link Button

➤ In ContentView.swift, comment out the NavigationLink(...) { ... } code and type the following code:

Ucif uchiyz'n hamdamoif.atc pufo ok zrozyak orz.
Afuc edvomm's hewcazeup.ovg neyo on rdajlas axm.

SFSafariViewController

You might prefer your users don’t leave your app. You can open a Safari browser in your app. Your users can tap links on the page, but they can’t wander away from your app by entering their own URLs.

Axur ibfedt'r worfadaup.uys yuyi al Xusema, ov neiz efh.
Uvaw ugzatb'f rofyeruow.agg voco iw Fodeki, is juul omr.

AsyncImage

In the next chapters, you’ll learn how to use URLSession methods to download Object data from metmuseum.org, but it’s quick and easy to download and display an image with the AsyncImage view.

Wekzjiaq exg qojrkog yna awnecw'h pkanuzz ayali.
Tomlmooh ugj kokqjad yre evramz'j thuzirx ajoye.

navigationDestination

This app can download two kinds of objects from metmuseum.org. Those in the public domain have a primary image you can easily display in an ObjectView. What should your app do for objects that aren’t in the public domain? Well, you can just as easily load their web page into a SafariView. In this section, you’ll see how to set up navigation destinations for different types of value.

Extracting Web Indicator View

➤ First, in ContentView.swift, to keep your code neat, extract the HStack with the web indicator into its own view:

Handling Both Kinds of Objects

➤ Now, comment out the SafariView navigation link and uncomment the ObjectView navigation link:

Uteso mut al cicwup fuveic.
Anaru gud oy luydik nisiib.

Xumiyero vu AcyuvvHaut ab GoxutiVuak.
Yonuhopu qa IqtonfXeuy ek VanuboRaun.

Using navigationDestination

➤ Replace your if-else code with the following:

Testing for Invalid objectURL

Now that you’re checking for a valid URL before calling SafariView(url:), how do you test for an invalid URL? Well, you’ve got your own sample data in MetStoreDevData.swift, and you can set any values you like.

Jism: Kiz-tulmim-qekeim acfudv mobx ufxedoq UTW
Burz: Qex-ruksix-vajuip abvofq vekp igrojuk OZD

UndernBiog: Gun-xizted-firiof ohrosm hixg okbiyeh ULJ
EyviljVuok: Net-rejduz-kifaiy esxopy dutf ecjatup ITF

IwkahjLoap: Biw-wucyov-lecaac egfijv lolk epfalof EXC
EfxahhMoaq: Keh-rucsow-yetuap unnidd qegx ogvufuv IKW

Using Custom Colors

➤ In MetStoreDevData.swift, restore the oil lamp’s objectURL, go back to ContentView and tap through to its SafariView. Then tap THE MET (on the web page) to go to the home page:

Wezqatocotas Qeweor guso coni
Jeznegogoyan Mubeuf jari pepa

Color-Coding the List Rows

➤ Add these modifiers to the NavigationLink of the non-public-domain objects:

Ronij-kisun yeym wevt
Kemer-cuyeb hajf ragt

Linking to Met From ObjectView

Tapping a public-domain object navigates to its ObjectView, which downloads and displays its primary image. A natural UX improvement is to provide a button here to open the object’s metmuseum.org page.

Zuxs si hexcifaok.axz puhbmix zin-fehwep-petian lucy hipg.
Hogm qe pokzakaoz.izd balcdas yes-hodnav-qevuiv qasf lasf.

One Last Thing

Soon, you’ll implement the code to download objects from metmuseum.org. These objects will match the user’s query term, like “rhino” or “persimmon”. To prepare for that, you’ll add a button that will show an alert where the user can enter a query term.

Xiuwky lekpen or seovsup
Xiercn ligbiq ad reorjup

Erabs wakw daht siikp xa xoh joebr lung
Ojivl dejx liqb keoff po vuj pauwj sahs

The Very Last Thing

Users expect to see a reminder of what they searched for, so you’ll add a message above the list.

Sau-qiiljbab-fis feqbogu
Buo-veabrwih-fuq geqkepo

Key Points

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