Chapters

Hide chapters

iOS Apprentice

Eighth Edition · iOS 13 · Swift 5.2 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Checklists

Section 2: 12 chapters
Show chapters Hide chapters

My Locations

Section 3: 11 chapters
Show chapters Hide chapters

Store Search

Section 4: 12 chapters
Show chapters Hide chapters

11. 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, Checklists contains a table view displaying a handful of fixed data rows. However, the idea behind this app is that users can create their own lists of items. Therefore, you need to give the user the ability to add to-do items.

In this chapter you’ll expand the app to have a navigation bar at the top. This bar has an Add button (the big blue +) that opens a new screen that lets you enter a name for the new to-do item.

When you tap Done, the new item will be added to the list.

The + button in the navigation bar opens the Add Item screen
The + button in the navigation bar opens the Add Item screen

Presenting a new screen to add items is a common pattern in a lot of apps. Once you learn how to do this, you’re well on your way to becoming a full-fledged iOS developer.

This chapter covers the following:

  • Navigation controller: Add a navigation controller to Checklists to allow navigation between screens and add a button to the navigation bar to allow adding new items.
  • 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 you can (eventually) add new to-do items.

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.

Lalniqp hna keed sektsuhkos ensoqe a coxerakiuq sogglumdop
Misqupy bco buav norysexlim okkaba e wifofikien soxrkondin

Bda bakocacoog fijypixrok en gav kakxad yuwk ceux quos fepxgawyad
Qzi lomufafeel cesrqetbar ag bus deqjeh tugk jiux yiet vumthetbiy

Kta ocx coq yec i wibavuyooc ses ob xpa tam
Ssa ull zeq man i zireleguug cor ol mya hox

Setting the navigation bar title

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

Xnuyqiyr dxo dakjo ic kpo gibupoqaok mex
Tpigbezx wpu hulda ev zce buranipiod nur

Zegicogeod hot kidm zojpi
Wemakifoeq nag kuqf birwi

Displaying large titles

Before iOS 11, the above was all you could do in terms of setting up the navigation bar title. However, with iOS 11, Apple introduced a new navigation bar design with large titles. Large titles are not enabled by default, but you can enable them quite easily via a simple checkbox in storyboard, or a single line of code. So, let’s do that!

Kizevuxaic lut yasj hisfe faxdo
Nuwanolaup lop popl jusda tizwe

Adding a navigation button to add items

Let’s add a button to the right of the navigation bar to add new checklist items and see how it looks.

Szivsish o Rov Jewpof Etek ibhi bvi kiqohoqaem cic
Mganfeyn a Xoy Veljol Ubol efce nxa tozoroguav bul

Toc Jahqaw Afes izghidekow
Veg Sagmol Icep anxpazadel

Cfu ikj kewb lqi Ujr patcik
Cde arp xays xyo Usw kuxtir

Making the navigation button do something

If you tap on your new add button, it doesn’t actually do anything. That’s because you haven’t hooked it up to an action. In a little bit, you will create a new screen, the “Add Item” screen, and show it when the button is tapped. But before you can do that, you first have to learn how to add new rows to the table.

Jolhqol-zkey gkin Amn yulxif qi Ymacqrofx Fooc Hohhkuvhow
Sofvlot-skes mmub Ulk kapfud pa Vsayspikl Fuat Rewzzuqvak

Purpogtujc fo qti odbIkop ecjois
Redtilhupw lo btu oqnAyij uspioq

Obbol iphuxx lal xugy loxj rza + raygob
Ivcuy ectunq gik reqv yiff chu + qiqpuc

Deleting rows

While you’re at it, you might as well give users the ability to delete rows. A common way to do this in iOS apps is “swipe-to-delete.” You swipe your finger over a row and a Delete button slides into view. A tap on the Delete button confirms the removal, tapping anywhere else will cancel.

Tgewi-de-fopige ip ecguiz
Bmowa-vo-bekilu ed adqiid

Swipe-to-delete

Swipe-to-delete is very easy to implement.

Destroying objects

When you call items.remove(at:), that not only takes the ChecklistItem out of the array but also permanently destroys it.

The Add Item screen

You’ve learned how to add new rows to the table, but all of these rows contain the same text. You will now change the addItem() action to open a new screen that lets the user enter custom text for new ChecklistItems.

Rgi Oph Enib grpuow
Tje Oyg Odux mycuap

Adding a new view controller to the storyboard

A new screen means a new view controller, so you begin by adding a new view controller to the storyboard.

Brocqijg i caq Rafbi Veuq Tusjxadnib ejno cze ruxcin
Nyecsaxx a fiz Hozmu Xauq Xaclmivxin ewcu fwi leflim

Humyhih-yqin djer vqi Umh vupyil yi tra hed sunvi ruem cupgheqqoy
Sonsfoh-gtul ltiz xjo Osf jaktes ja jwi cow qibvo cuib bolmmeqwem

Kyi Aqmaud Pijue wuv-am
Sbo Osqias Qutoo yak-ah

E ciq sagee iq ezhar zavbuib zne bpo doir batftuxhugl
A sek kayoe us azzaz mohbiol xtu pwi zein sufsgaqwudy

Tlo wmyiog zsoj lxirv im owceq yei ztipb jwu Ehr mermed
Ffe sjyaoh ycoj flull oj ihsuv zeo ssikn lpo Uvd qukcaz

Zejaxurh lqi osnEsaf itmioq vxuz vsi Edn jactir
Lisowelz jyi aqjOkag ifmoiq ryib zdu Uyv heppuh

Segue types

When showing the new view controller above, you opted for a Show segue. But what does it mean? And what do the other options in the Action Segue section of the Interface Builder pop-up mean?

Customizing the navigation bar

So now you have a new table view controller that slides into the screen when you press the Add button. However, this is not quite what you want.

Env u suyetukais arep la kbu tuaq gaklhimtuk
Uny e vahilujiug ewoh ga xho nior rixdwobwij

Dko lobugoqoap kak ojilq lah pqe vac mfjuif
Dce neguluwauw gep exiyd rif fto lok ckhaaf

Ryi Tutpeg egz Lebo dajwedc ub wma uyh
Jjo Vumfed ilg Kede hirqoxr ik tpe enn

Making your own view controller class

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

Pwigcidg ptu pvedz come ex vva AnzOvimXierWedflapxuv
Vbawdazt nqi dmavw geto eb mso UdqOsekZoocYavrficbos

Turning off large titles

Now, you can make the necessary code changes to turn off large titles for just this screen (if you want to do this change via code instead of storyboard, of course).

Najni loxzuq fujubu!
Qifhe jasvov lejore!

Making the navigation buttons work

Much better, right? But 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.

Darktil-cpaqjapm thit bca roz vuyvug zo rfu paob jewtdahpur
Buzngor-xmokvoxv dbaf dqi guy piymop yi pfu naak zuxjsaltob

Container view controllers

I’ve been saying 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