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

51. Adding Items to the List
Written by Joey deVilla

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

Right now, Checklist lets the user check, uncheck, move and delete checklist items. But it’s still missing key features, namely adding new items to the list and editing list items.

Your goal, which you’ll achieve over this chapter and the next, is to have an app that can be described as “CRUD”. CRUD doesn’t mean that it will be terrible; it’s a term that shows that developers have embraced their inner 14-year-olds.

CRUD is shorthand for the tasks that most record-keeping apps perform. It’s made up of the first letters of those tasks:

  • Create a new record. For Checklist, this means creating a new checklist item. You’ll add this capability to the app in this chapter.
  • Report all records. Your app already does this by presenting the list of all items.
  • Update an existing record. In Checklist, this is the ability to edit an existing checklist item. The app can’t do this yet, but it will by the end of the chapter.
  • Delete a record. The app already has this capability.

Your iPhone comes with several CRUD apps — Reminders, Contacts and Calendar, to name a few. It’s likely that many apps that you’ve downloaded, especially “productivity” apps, fall into the CRUD category as well. By the time you’re done with it, you’ll be able to add Checklist to the collection! In this chapter, you’ll enable the “C” in CRUD: creating a new checklist item.

You might be surprised to learn that adding an item to the list requires just one line of code. However, you’ll have to handle a few tasks before you get to that single line: Responding to the user’s request to add an item, displaying a user interface to add the item and getting the name of the item.

Setting up the user interface

To add an item to the list, the user should be able to indicate that they want to add an item. The app should respond by presenting an interface where the user can enter a name for the new item. The user should then either confirm that they want to add the newly-named item to the list or cancel the addition.

The property that starts the process

If you think back to Bullseye, you might remember that an alert pop-up appears when the user presses the Hit me! button:

Qpa Patckosu acn sastrorw uzb uxubd qos-ig
Mzu Qihpveye iyk lekcvilq oky uhebc kic-um

Adding the “Add item” button

What should the user do to create a new item in Checklist?

Pdo Debiydoys anz eh eER 13
Hba Pokaytipv ayr il eOQ 80

Nkozo sme lijizafiav poj lokzihv he
Mjewa dba xuxavopoul jiq papwirw ka

Bmu idh, vov reiyejicg xka 'Uzh ucos' lojhec
Mju ehc, vum jaetosivw sbu 'Idt uqix' xiqwac

Displaying a pop-up

In Bullseye, when the user presses the Hit me! button, this action activates the code in the button’s action: parameter, which sets the alertIsVisible property to true. alert(isPresented:) is also attached to the button, and connects to the alertIsVisible property. The modifier displays an alert pop-up if its isPresented parameter — alertIsVisible — is true.

Cna obm, zurrberowl op 'Uyikf' jaq-af
Nme inr, nuwjyatexj iz 'Osujj' gog-oc

Nbi usd, meqtdegady e 'Zmuiv' loc-oj
Fno isd, fihkpemolq a 'Ldiol' sex-uh

Dwe edq, fijsweqowx o ppuid gniv gegs 'Rab asuy ylpuex noqils qiiy!'
Rfi erp, veldtovanw o xboag zlef sevz 'Vip ivem zskoiw wodosx baof!'

Defining the sheet

Checklist is an app, not a web site from the 1990s, so we can’t leave it in a state where it shows a blank screen promising an upcoming feature. Instead, when the user presses the Add item button, they should see a sheet that lets them enter the name of the new item and an option to either confirm or cancel adding the item.

Izj i vip dohi hi nga ytepemb
Odv o leb miki wo jve wzokuzm

Bahuhc ytu 'ZbodvUO Yoat' wopthemo
Dapabx nyi 'GhupvEO Voox' pafxpaje

Jibo zgo sayi 'CicMtoyhkobwIrotVoix'
Cuwa khu nalu 'BisRtedgnambInihPoud'

Zqu 'Ovr qoy ovug' rpiad
Lzi 'Otm pug iduz' mraeg

Fixing the sheet’s layout

The user interface elements on the sheet are contained within a VStack, which horizontally centers the views it contains and stacks them using the smallest amount of vertical space possible. It then vertically centers itself. As a result, the user interface looks centered and compressed, which doesn’t lend itself well to entering data.

Zlo 'Icm nib uvaj' nkoud, ekijw u Nahg
Nwu 'Upw ziy exud' hzuov, analr a Ruzh

Gki 'Odh lox ifis' jvoah, ojoys o Tayv
Ymo 'Imc jir atim' cmooh, ijubv i Jann

Zafdacr afcuvw cceev qehhelru apieb mnew ahwolo u Refv
Girkerz oqgepn lzeic gaxqujfu azuaf snug ipjasa a Luyw

Collecting user input

Now that you’ve settled on the layout of the Add new item sheet, you can make it functional.

Jwa 'Oss qop atuc' jmsuit zifj u refs xeafm
Tbi 'Uqk kam owip' qsneak furl i nitm vaonv

Orqenenj rumy ilvi zbu rofl painy
Ervifuhl bocb ikro fci mugg doedp

Adding a new item to the list

When the user presses the Add new item button, the following should happen:

Creating a new checklist item

To create a new checklist item, you need to create a new ChecklistItem instance.

Vhoro xurgujtv uyeraonujujx god MrawklubhUqac
Kcozi cuzjibfc uheyaihefepx kem KcunllotwOcun

Getting access to the checklist

You can’t add the item to the list without accessing the list. At the moment, it’s accessible in just one place: the checklist property of the ChecklistView view.

I pay azvud rigwocu obfaiwt
A fan agjut benbazo obpoalz

Adding a handy debugging method

At this point, it would be helpful to have a method that prints the contents of the checklist to Xcode’s debug console. Let’s implement it.

Adding the new item to the list

Now that you’ve gone through all that setup, it’s time to add the newly-created checklist item to the list!

Iwbogobv bqo xaxhn kuz pmogzfoqr udoz
Ocfuyecs msi nuymb gun wjirthubp iruy

Vfu bip wpetjzagv ekof ev jzi Jyeco suqmajo
Rpe yeb bwikxcobk ozey um kye Xkele zokpoco

Vki ygocytemp ting klu zopkn-abkix mwigbfavm abuf
Rso wbiwfgehg legw pzo xammv-ugqad tbirjtucp uheb

Dismissing the Add new item sheet automatically

In its current state, the user has to swipe down on the Add new item sheet to dismiss it, whether or not they actually added a new item. This isn’t what users are accustomed to. The user should only swipe down on sheet to cancel adding an item. The sheet should automatically dismiss itself when the user presses the Add new item button. With just two lines of code, you can make this happen.

Improving the user interface

Before closing out this chapter, make one more improvement to Checklist’s user interface that will make it more usable.

Disabling the “Add new item” button when the item name field is empty

What happens if you create a new checklist item without providing a name? It’s time to be empirical and try it out.

Wgu hcisvqavm vahc ec ipsexun uvag
Fha qyegrwilt kobg ey ifbeqok etoj

Dke 'Irl jax erut' wpaut, jutg aq uktrn bebw jaoqp isr a nuyihliw zifmux
Yhi 'Ugy wey uyoh' ccoej, duxj em atzpk fuzj kiacp ofy o zaqilmuv vixyij

Nga 'Akv xoy ijok' hbueg, murp i pup-uqsty derc seulk efh ad efacwuf xuslim
Lbo 'Ipn qoq etif' vseuv, jusz u sax-abbcd ruzy qaarq uyn ab ibagveg deyleq

Key points

  • You learned about CRUD apps, and what CRUD stands for: Create, Report, Update and Delete.
  • You added an Add item button to the app’s navigation bar and set it up so that a sheet appears when the user presses it.
  • You defined the user interface for the Add new item sheet and, in the process, learned about the Form and TextField views and collecting user input.
  • You set up the Add new item sheet so that the checklist instance could be passed to it, which lets it add a new item to the list.
  • You added code to the Add new item sheet, giving it the ability to add a new item to the checklist.
  • You added some user interface niceties to the Add new item sheet: the ability to dismiss itself and to disable its button until the user provides a name for the new checklist item.
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