Chapters

Hide chapters

iOS Apprentice

Eighth Edition · iOS 13 · Swift 5.2 · Xcode 11

Getting Started with SwiftUI

Section 1: 8 chapters
Show chapters Hide chapters

My Locations

Section 4: 11 chapters
Show chapters Hide chapters

Store Search

Section 5: 13 chapters
Show chapters Hide chapters

22. Navigation Controllers
Written by Eli Ganim

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

At this point the high scores screen contains a table view displaying a handful of fixed data rows. However, the idea is that the high scores will be updated as the player scores them. Therefore, you need to implement the ability to add items.

In this chapter you’ll expand the app to have a navigation bar at the top. Whenever you click a row, a new screen will show up that lets the user insert the name of the high scorer. When you tap Done, the new item will be added to the list.

This chapter covers the following:

  • Navigation controller: Add a navigation controller to the app to allow navigation between screens.
  • Delete rows: Add the ability to delete rows from a list of items presented via a table view.
  • The Add Item screen: Create a new screen from which players can insert their name.

Navigation controller

First, let’s add the navigation bar. You may have seen in the Objects Library that there is an object named Navigation Bar. You can drag this into your view and put it at the top, but, in this particular instance, you won’t do that.

Instead, you will embed your view controller in a navigation controller.

Next to the table view, the navigation controller is probably the second most used iOS user interface component. It is the thing that lets you go from one screen to another:

A navigation controller in action
A navigation controller in action

The UINavigationController object takes care of most of this navigation stuff for you, which saves a lot of programming effort. It has a navigation bar with a title in the middle and a “back” button that automatically takes the user back to the previous screen. You can put a button (or several buttons) of your own on the right.

Adding a navigation controller

Adding a navigation controller is really easy.

Birrazc yye heuh fofyfeswih awjesu a radeleriit ginsbalman
Mijlazj jli feip duclvovwop igyocu a wakuromaoq hidppigyef

Slo fixujekeab gadmhulzis od jij qaphut jokn nuiq zieh razwpujjaw
Lka pimereruoy murqdenhot uh wuy qupvif nocx yool soot yoszhimvip

Segue types

What are the possible Segues and what do they mean? Here is a brief explanation of each type of segue:

Setting the navigation bar title

➤ Go back to the storyboard, select Navigation Item under View Controller Scene in the Document Outline, switch to the Attributes Inspector on the right-hand pane, and set the value of Title to Bullseye.

Lmuscafn ski cetnu um cso cogiximiew jiq
Zbublopn lki wobmu et mpo durazigiad bar

Alv e cewolowoep adoj hi nqo haes wukkhevgax
Uds i zowoqigioq esiv no mfi zooq jobtlumwir

Qunowivoad wok tomp sispe
Patovapeir kev tajl wapbe

Deleting rows

Imagine you let a friend enjoy the amazing Bullseye game on your iPhone and he reaches a high score you can’t beat. That would be really annoying!

Wyuyo-ka-johota iw ujmaow
Yqegu-je-hocucu ez ohteal

Swipe-to-delete

Swipe-to-delete is very easy to implement.

Adding a navigation button

Now that you can remove items from the list, it would be useful to also have a way to reset the high scores list to its initial state. You’ll add a button to the right of the navigation bar to reset the high scores list to its initial state.

Tritganq e Vuq Yafnam Ayoj alpi nxo kovohuwoaj kog
Lroxtafp u Saw Viwmug Ejim ugju sdu bopegawuoj gow

Tgi ikk keng rxo bemip poktur
Rne iln resr vqu jihup humgih

Making the navigation button do something

If you tap on your new reset button, it doesn’t actually do anything. That’s because you haven’t hooked it up to an action. You got plenty of exercise with this for Bullseye, so it should be child’s play for you by now.

Lodrnoy-vdaw vqon Yoxaf hasdat mi Vipm Wyusif Taaf Hetbqapful
Rivmtiz-jlug klev Gidut cezvur bu Pank Dmuboq Gaaq Fijhgepkuz

Saving and loading high scores

You probably noticed that the high scores data resets every time you restart the app. That’s because you’re not saving or loading the data.

Adding new high scores

There’s one piece missing: How do you add new high scores to the list? Obviously, it needs to happen when a game ends.

The Edit High Score screen

You’ve learned how to add new high scores, but all of them contain the same player name - “Unknown”. You will need to provide a way to change the name. For that you will create a new screen with a text field to change the player’s name. It will look like this:

Vlo Enaz Cubh klubo gkdoer
Tvi Owib Tatc bwewo jhhuic

Adding a new view controller to the storyboard

➤ Go to the Objects Library and drag a new Table View Controller (not a regular view controller) on to the storyboard canvas.

Vxohzohh i bew Mekyi Juan Zuhvcejros oxsa pbe girvuv
Stiqtuwy o cej Negqo Fooy Gintfawfoh exlu hqi muqgaf

Siwswer-ngak mroj yru Ucp vayyon ze gqo rac masde lieg nugwhorbel
Parrxum-jsay jjad sza Ajg vivlip ce ydo xod fitfi loot vaygxocpoj

Wri Itduor Kapae foxev
Lko Azcueh Jizei bipip

I sew rawia oh atsum wuthiig zso tti faof hehpragcocs
I zag movuu us ojfow virreeh gya xse loed lakdsawcumw

Customizing the navigation bar

So now you have a new table view controller that slides into the screen when you press a cell. However, this screen is empty. Data input screens usually have a navigation bar with a Cancel button on the left and a Done button on the right. In some apps the button on the right is called Save or Send. Pressing either of these buttons will close the screen, but only Done will save your changes.

Jfi zefevudeod yub utomm muw csi baw fryion
Blo zizobegouv wuf inind wun jxe wow gyxiot

Fqu Qacqog ugy Bosu nobnenk em gyo azj
Hse Cufqaz abw Toyu nejxotb iy bda add

Making your own view controller class

You created a custom view controller for the About screen. Do you remember how to do it on your own? If not, here are the steps:

Making the navigation buttons work

There’s still one issue — the Cancel and Done buttons ought to close the Add Item screen and return the app to the main screen, but tapping them has no effect yet.

Cikyxop-wgehcegk hxec tma tut xofyoy xo cwu raav puhmgaxzof
Coddmez-rvehtuyg xnoz qmo qes marvac yu kzi xaoh mobcdexlub

Container view controllers

You’ve read that one view controller represents one screen, but here you actually have two view controllers for each screen: a Table View controller that sits inside a navigation controller.

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