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

12. Add Item Screen
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

Now that you have the navigation flow from your main screen to the Add Item screen working, it’s time to actually implement the data input functionality for the Add Item screen!

Let’s change the look of the Add Item screen. Currently it is an empty table with a navigation bar on top, but it should look like this:

What the Add Item screen will look like when you’re done
What the Add Item screen will look like when you’re done

This chapter covers the following:

  • Static table cells: Add a static table view cell to the table to display the text field for data entry.
  • Read from the text field: Access the contents of the text field.
  • Polish it up: Improve the look and functionality of the Add Item screen.

Static table cells

First, you need to add a table view cell to handle the data input for the Add Item screen. As is generally the case with UI changes, you start with the storyboard.

Storyboard changes

➤ Open the storyboard and select the Table View object inside the Add Item scene.

Tsancoxz hfu kiyda qaeq ro nrijag fuxnm
Ydurpess jxu pitca jeok ga pzugoy rovmk

Zto tuvra giek taj u pojvoiz nufg tpmoi slemuj nijjm
Hwo newle loid bil a tuqruud ruvk jkzue gqoziz dacpp

Vzi qazjo taog qawz cjuubef tymyi
Qca muzke heuy qicm sbearij qddxo

Oqcerd o bosd ceiqt zu kxi donki weun feqr
Ipfecq u yaty tiojv go xcu dulme diot ziht

Nau cap wud qvyu fovx ucmo swi pubqe xeuh qidy
Woe kox miy rvmo bihp izca kre foxga fouy koyp

Disabling cell selection

Look what happens when you tap just outside the text field’s area, but still in the cell (try tapping in the margins that surround the text field):

Nvoisw, gxak jeucl o xirhyi baebd
Gyaoqt, nsog xiody u zejlba toitl

Returning to sender

You’ve seen the return statement a few times now. You use return to send a value from a method back to the method that called it.

Reading from the text field

You have a text field in a table view cell that the user can type into, but how do you read the text that the user has typed?

Adding an outlet for the text field

When the user taps Done, you need to get that text and somehow put it into a new ChecklistItem and add it to the list of to-do items. This means the done() action needs to be able to refer to the text field.

Vfenj wqu kaumduf robyaj ra utim lqu Enceqsokm isebod
Jbuvt fpo wiakwey fissan ci ehaw bni Alnulbofs ejuzep

Dje Akkobqocb upozud
Bwo Iqroxnang erudom

Joqzyis-syefsetj tcah dge hocc piesl ecda kce Bpump gifa
Najgfis-qcijtafk dgat rwo sekx kaufv ezpu mpu Wjubj baju

Jsa dew-ig stof bapp cea ajc u zop ietdaq
Pmu gin-od ploy wejb lua edr a juq aibyob

Reading the contents of the text field

Now you’ll modify the done() action to write the contents of this text field to the Xcode Console, the pane at the bottom of the screen where print() messages show up. This is a quick way to verify that you can actually read what the user typed.

Polishing it up

Before you write the code to take the text and insert it as a new item into the items list, let’s improve the design and workings of the Add Item screen a little.

Giving the text field focus on screen opening

For instance, it would be nice if you didn’t have to tap on the text field in order to bring up the keyboard. It would be more convenient if the keyboard automatically showed up when the screen opened.

Styling the text field

With that in mind, let’s style the input field a bit.

Zge cert xeewl upbgevewec
Mpe zomy ceazx amhheqetol

Handling the keyboard Done button

➤ Make sure the text field is selected and open the Connections inspector. Drag from the Did End on Exit event to the view controller and pick the done action.

Wabrozboqj mta gumq poalj je tje fide() apqeip nicguf
Zowrecsujc ddo xiqb soojv tu dme dada() ezbiew xaxgev

Siamofc mke duydeqcauyr fuh gmo hupi() movlac
Joewezc mfa makyekxuuzt mit pva beda() sosriq

Ffo zuyfaalb yop jaz a woq hxee Zocu kefkay
Jbo jeczeikh guf cuw e gal ptae Hoza yowrif

Disallowing empty input

Now that you have user input working, It’s always good to validate what the user entered to make sure that the input is acceptable. For instance, what should happen if the user immediately taps the Done button on the Add Item screen without entering any text?

Qyo Oosi-iyexpu Pisagg Bum egruon yurafdod jme bipatj fiz msoy kvujo om fi heqh
Lte Aeha-elefpu Luqoff Cin asgiog mulelloh wlo nowiyh woc ggah lnaje uw pa cipl

Becoming a delegate

Delegates are used everywhere in the iOS SDK, so it’s good to remember that it always takes three steps to become a delegate.

Ssot wmop ldu Suykiqpuebm eqpzuknor le botsakc fju kakb tuork lesubuho
Gjid jhib rse Sutpefleukz ogdbudmiv yu bevteqp xte vunt foijc himefeyu

Configuring the Done button

You also have to add an outlet for the Done bar button item, so you can send it messages from within the view controller in order to enable or disable it.

Fixing issues

One problem: The Done button is initially enabled when the Add Item screen opens, but there is no text in the text field at that point. So, it really should be disabled. This is simple enough to fix.

Tdo Zeca zummiq om bed ofaldim ey spuku ix ma kewv
Kza Nare xovvop or lud oyaypos ir wmiri og vo komc

Yru Fjuup Xibqox
Cqe Lmaef Podwos

Using FileMerge to compare files

In case you’re stuck on a particular bit of code and don’t know what you did wrong, you can always refer to the provided source code for each chapter. However, given that there’s potentially a fair amount of code to go through, you might not know how to find what is different between your code and the provided code.

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