Chapters

Hide chapters

UIKit Apprentice

Third Edition · iOS 18 · Swift 5.10 · Xcode 16

My Locations

Section 3: 11 chapters
Show chapters Hide chapters

Store Search

Section 4: 13 chapters
Show chapters Hide chapters

13. Delegates & Protocols
Written by Fahim Farook

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

You now have an Add Item screen showing a keyboard that lets the user enter text. The app also properly validates the input so that you’ll never end up with text that is empty.

But how do you get this text into a new ChecklistItem object that you can add to the items array on the Checklists screen? That is the topic that this chapter will explore.

Add new ChecklistItems

In order for a new item addition to work, you’ll have to get the Add Item screen to notify the Checklist View Controller of the new item addition. This is one of the fundamental tasks that every iOS app needs to do: sending messages from one view controller to another.

Sending a ChecklistItem object to the screen with the items array
Sending a ChecklistItem object to the screen with the items array

The messy way

Exercise: How would you tackle this problem? The done() method needs to create a new ChecklistItem object with the text from the text field (easy), then add it to the items array and the table view in ChecklistViewController (not so easy).

Mpmoiy E bfenx ejm uloun dkjaoz G, pey V pfoyz nusnejc iy O
Gzziiv I cvurt ips isiat fzjauq F, xiy Q ghiwl wapnopw is A

The delegate way

You’ve already seen delegates in a few different places: the table view has a delegate that responds to taps on the rows; the text field has a delegate that you used to validate the length of the text; and the app also has things named AppDelegate and SceneDelegate — check the project navigator.

Vxhuid A waotkcud djrois Z igs tevowok azm xabozepe
Lfdoif U woujbjus sjcoam X ozq qosojow esd putelisu

Bcod it nhub Nbsius J suex: opst yki sezaduwa kerm, wij mji butw af wqzuir E
Xrip of zmig Ldjeew G seig: ibqw rsi jiyinusa hipj, yiw tba bihf ut tlziuk I

The delegate protocol

➤ At the top of AddItemViewController.swift, add the following after the import line, but before the class line — it is not part of the AddItemViewController object:

Protocols

In Swift, a protocol doesn’t have anything to do with computer networks or meeting royalty. It is simply a name for a group of methods.

Notify the delegate

You’re not done yet in AddItemViewController.swift. The view controller needs a property that it can use to refer to the delegate.

Optionals

I mentioned a few times that variables and constants in Swift must always have a value. In other programming languages the special symbol nil or NULL is often used to indicate that a variable has no value. This is not allowed in Swift for normal variables.

Conform to the delegate protocol

Before you can give AddItemViewController its delegate, you first need to make the ChecklistViewController suitable to play the role of delegate.

Tnozu kexnv ehaem cal zucrulruwp ce hdejijug
Vguxu sinyz ogair suj fayjizkotf pa cyoharub

Set the segue identifier

See the segue identifier mentioned in the code above? Where was it set? The answer is, that it wasn’t! We need to set the identifier in order for the above code to work. If you forget to, then you won’t get the delegate set up correctly when segueing to the Add Item screen.

Reyujn pwi wikeo lopgeub ncu Tpokpnihzs dsazu agt sya Itp Iwib ygayi
Feqodd wpe fimii gugvook ftu Szafbsibzn hjaro oyw bwa Uhv Awik fpuki

Add new to-do items

➤ Change the implementation of the didFinishAdding delegate method in ChecklistViewController.swift to the following:

Gou cef xefoxkb ujg toc arorv ve bto ru-ta vodl
Kiu raj foruhsx ozj loz uzals jo pyu bi-ro fakw

Weak

I still owe you an explanation about the weak keyword. Relationships between objects can be weak or strong. You use weak relationships to avoid what is known as an ownership cycle.

I X wwdimz zkbazw

E V sglatb coic

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