قالب وردپرس درنا توس
Home / IOS Development / Shipping data between controllers: Delegates, Notifications and Close

Shipping data between controllers: Delegates, Notifications and Close

Long time do not see dear readers and Appcoda fans! It's been a long time since my last post here at Appcoda, and I'm very happy to be back!

Today, I am going to discuss an important and definitely interesting topic that focuses on a fairly often overlooked concept. And this question is often asked by beginners:

How do you implement communications internally in apps and exchange messages properly between classes or other devices in Swift projects?

It is undoubtedly a must-have knowledge for each developer, new or not. Knowing what tools and techniques are available, what works best, where and when, is critical to a proper development process. After all, being able to send messages and data back and forth in classes is not something that one can possibly learn; Instead, it is one of the first things a developer should know about.

So in this post we will see the following methods that allow communication in the app:

  • Delegation pattern
  • Alerts
  • ] Closures and action handles

Usually, not all of these three concepts are met in One post, but one can find a lot about each topic separately. I felt the need to write a training that concentrated all of them as they actually serve the same purpose; Let us exchange messages and data between our programming structures and devices. Through simple words and practical programming examples, I will try my best to make it easy to understand how each concept works. Hopefully, at the end of the training, some valuable lessons will be learned.

A programming task without some coding doesn't sound right, so we will work on a demo application that lets us meet each concept as thoroughly as possible given the boundaries of a single post. For sure, what you want to see next is standard techniques that you just have to adjust and use in your projects. And by saying that, let's not lose any time here and let's move on to the interesting things! Happy reading!

A start project for … Start with

We're supposed to make our hands dirty, but we don't want to write a full app from the beginning. We just want to focus on important things, as the rest is already done in a start-up project that you should come from here.

If you assume you've downloaded the startup project and opened it in Xcode at this time, it's time for a quick trip. Our main goal today is to build a handlist app as much as possible so we can use all principles we will learn about delegates, messages and closures. At the end of this tutorial, the demo app will be able to:

  • Create shopping lists.
  • Keep and view a collection of all shopping lists added to the app.
  • Choosing a shopping list and displaying their items
  • Editing an existing shopping list.
  • Rename and delete shopping lists.
  • Add, edit and delete shopping list items.

If you run the Startup project, you don't see much on it. Yes, you can navigate from one viewer to another, but no data can be stored, nor is there any other significant operation you can actually test. And that's because important parts about communication between app classes are still missing.

It would be more interesting to go through the code though. First and foremost, you will find three different display controllers:

  • AllListsViewController : This is the place where all shopping lists are collected and displayed. From this view control, you can start creating a new list, edit an existing one, rename and delete.
  • ShoppingListViewController : It publishes all items in a list and lets you create, edit, and delete items.
  • EditItemViewController : The name suggests what is about. You create, edit or delete item list items.

In addition, there is a file named ShoppingList.swift containing two structures:

  1. ShoppingList : It represents a shopping list programmatically and contains properties for: id, name, edited timestamp and elements of a list.
  2. ShoppingListManager : It manages a collection of ShoppingList items, and it offers many useful features. An instance of this structure is used in AllListsViewController display the controller. It is called listManager and it is our tool for managing shopping lists.

You can also find a file named NotificationNameExtension.swift . This file is empty, but soon enough will change. More details later.

Finally, there are custom cells and custom display implementations. What is particularly interesting among them is RenameListView a custom view that allows you to rename a shopping list. Well, not yet, but at some point it will. Remember that, when we are going to work on it when we are going to discuss closures and actions.

Take the time to familiarize yourself with the startup project. Go through the code, examine the display boards and other classes or structures, and find out what is offered in the startup project and what not. When you are ready, continue reading. Delegation pattern comes first!

Simple Word Delegation Pattern

Consider the following two classes, ClassA and ClassB .

ClassA initializes an object of ClassB with a view to using services that ClassB has to offer. We know that passes messages from ClassA to ClassB is straight forward as long as there are public properties or methods of access. For example:

As a common convention, a delegate has words as a suffix (it is not mandatory but recommended for clarity in code).

Step Two

ClassB declares a property of the custom protocol type:

Step four

ClassA assigns delegate property of ClassB [19659167]. In other words, it becomes ClassB s delegate. [Class 65A] Class
in it () {
// ...

classBObject.delegate = self

] {

init () [

// ...

classBObject . delegate = even



  • ] ClassB can use the protocol's functions to communicate with his delegate, who is ClassA . An advice: Always make sure that the delegate property is not zero .