Swift Data Transfer Between Pages

Hello friends, in this article, we will talk about how to send data between two pages with Swift. In the Het application, there is actually a communication between the pages. For example, if you have a list on the home page and we click on an element of this list, we should show the details of it.


In the example we will do, we will have a fruit model and we will talk about how many calories there are in detail. Let’s show the names of the fruits in our fruit list on our first page. When the list is clicked, let’s go into the detail and write how many calories there are in capital letters. Our design screen looks like this.



We need to create one fruit model. For this, it will be sufficient to create a model that keeps the name of the fruit and how many calories.


Now we keep a set of sample fruits and their calories in our Controller for listing. Then we connect it to our UITableView. We need to keep a variable for the selected fruit. We need to replace this variable with the fruit that the user chooses each time he chooses a fruit. After changing it, we need to run Segue, which allows you to go to our detail page from our list page. We gave this Segue the “showDetail” Identifier. After running Segue, we assign the variable we selected to the fruit variable on the detail page with the prepareSegue function.

After assigning the selected fruit to the detail page, it remains only to show it on the screen. We can do this inside viewDidLoad.

Our screen output is as follows.

If you have questions, you can reach by e-mail or comment. Good work.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.