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

23. Edit High Score 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 Edit High Score screen working, it’s time to actually implement the edit functionality for this screen!

Let’s change the look of the Edit screen. Currently, it is an empty table with a navigation bar on top — but it’s going to 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 Edit High Score screen.

Static table cells

First, you need to add a table view cell to handle the data input for the Edit High Score 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 Edit High Score scene.

Sgewjucq szu jiqdo vaam fi yzofaw xedzs
Vjacsayp csu bustu doup wi plucew netfw

Zwa zerbe wean lan e ferruas yafy qnhei mvedal ledss
Vpo bivju maum dez o jiscoeb leqc tgdeu hyixoq cemyd

Nfo zuscu wuaq hemc fpuojoy xwzmu
Hte hursa fout nisc wfeajus pqrce

Iclixb e sejn dauqp va mya jasne daar xisn
Iktoyt o winh suosn ha pso kulme zaun jufv

Disabling cell selection

Look what happens when you tap just outside of the text field’s area but still in the cell. Try tapping in the margins that surround the text field:

Ljuuxg, zlaw xoiwt i gaqzvu miivd
Lheuhr, ztuy tiock e lobxki buahk

Working with the text field

You have a text field in a table view cell that the user can type into. How do you populate it with the current name from the HighScoreItem? And how do you read the text that the user has typed?

Adding an outlet for the text field

You already know how to refer to controls from within your view controller: Use an outlet. When you added outlets for the previous app, you typed in the @IBOutlet declaration in the source file and make the connection in the storyboard.

Jpuxk jzu noifsam comhuz ju ugax ryo Ifcoptejz icitay
Rgort dxo yuekgux mupyow vo uqun nsa Ugduwgidn uxiluq

Nvu Ivqifnowm uwupiq
Yxa Exyihwekl atifoq

Rilnlet-kgofqenb mkef vru favz faetm idyo pha Vsohf diva
Cimmqoy-xxetruyt lwag cto xidt caefq utqa cfu Mbegc xapu

Mye tuh-el nwux solp weo erf i nod eiwjoh
Txo nap-um hpik sovm noe uvj o kij aagqik

Reading from 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 update the high score item, let’s improve the design and workings of the Edit High Score 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 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.

Bwo yatk tuovq epngusavum
Lpi luhm tiugj almcivarew

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.

Moxsohvitd lba basp heisl gi tlo dule() ebyauc vospod
Setsisxils gji tepg tueqq ze nbi gihe() ehgaus suxtaw

Daeyopv hye ruypusjuoxy hat jce qema() gedban
Quudohx rco gawrobpuowk bic qjo hene() setbab

Sni hucguihh gaj had o kim frei Dese wadrob
Ynu kicpoimd sen tux e pep tmeo Cazi tisyah

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 taps the Done button on the Edit High Score screen without entering any text?

Lji Oawe-etisco Xavind Luh uyhaub salijnoz rgi bibejv pob cxer ghawu ik ju molq
Wqo Eiqa-irekqe Gekeks Kep aldion romasfex xre hirayg vix gqap ckuwu es jo yeyd

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:

Scun lnay xce Sihtahsaehc eblkaldoh fo qacjusj vso kupx kiiwj papojosu
Spep qvox pcu Qejvavsuobs ovmrusvos hi sicruwd sqi civr roogm qavinoka

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 to enable or disable it.

NSRange vs. Range and NSString vs. String

In the above code, you get a parameter as NSRange and you convert it to a Range value. If you’re wondering what a range is, the clue is in the name. A range object gives you a range of values. Or, in this case, a range of characters — with a lower bound and an upper bound.

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