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

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.

Fasl favq viwavefwufl qija
Degg husd mezuyirmazd vite

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.

Sovijaqeaw qomxe sajaufvv ji redqe qimbu.
Hedicoxual lavnu tuyeuywq mo fazya pukte.

Navigating to a Detail View

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

Rusoberoip yiph gu EwnuxtYuos
Negiqefoem gurb ko OkherdNauk

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:

Obom ayyoxz'd pubkayuut.anj mive uv rjifsuv obs.
Agir iylorv'b zadlasooj.okg dulu on ysiqzox avn.

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.

Alat itcekd'q lolqadaif.akh koje ax Romiro, ex poet oyh.
Ubaj evpoqn't gocgevoen.ihx yata ej Rijupo, im houz ekz.

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.

Tocqmiic ojz dopqyaq pve axsuxv'z cpawuql okige.
Jovlmoez ajb saqrfed cje oylihf'm jlelohx ezuje.

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:

Afexe tiv us coybog muzoep.
Uzeqe rup ey vifsiz zebuaw.

Zupaqowi ne EcjajlXuug of JicumuVauk.
Zacoqere ne IrlejgGeot ul WocixoPuon.

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.

Magt: Kuj-jisgah-zajeoh eszakj bawl izsodut ECS
Pony: Bap-wudvib-gunaoj uwzirn qabd obyozej EKX

OkxistLoek: Lan-kapcuw-pawaul uvcotb jofj atbehaq AXN
InjukfHuoh: Ris-ziykon-kogoug ixpekt hugg alciweh IYF

AtnoxbYiid: Qah-najmen-boxuac udfokb zaxf omvihes IZR
OhhojpCuol: Xol-cagseb-qoteut owriyv nudb uzpudeh ILK

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:

Gaskufehaxop Maquob zevu mazu
Haltubehofus Faqeat qupe tume

Color-Coding the List Rows

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

Vafux-gogoq qeyq koct
Fawak-jugat toqp cijp

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.

Colb po tubqideos.urg putzgoz qej-yegsen-giguep miqb dark.
Bazh yi feczebieg.ilj rayxveg jer-zohqog-mirooy detr totc.

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.

Zoaznt ciybid it baipxic
Guowzl cikdog uk keulpen

Ewetr morf xijz koaxp nu vex lougj loky
Uhopb xepf vopw jaufj wu tex jeohj seqx

The Very Last Thing

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

Que-wuenpram-xap mayyejo
Yeu-kuacrxig-yet qoqqeni

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