iOS Tutorials: Store your favorite into your favorite list

OVERVIEW

Most apps now allow you to save your favorite but what is the best way to go about doing it. Thinking it through, it really is just a fresh new array to store your favorites. Let’s bounce right into it to make it happen

CREATING VIEW CONTROLLER

For simplicity sake, I will only be using two TableViewController and embed the first TableViewController with NavigationController and one BarButtonItem.

SAVING DATA TO FAVORITE LIST AKA PASSING DATA FORWARD

Make sure those view are connected with their respective classes and once we have those, let’s go into our first TableViewController which in my case is called MainTableViewController where all my datas will be created.

This is where I will create two Array

  1. The first array contains my data
  2. The second array contains my favorite list in which I store my data in it
These are some of the protocol that we inherited from UITableViewController and but I’ll only be focusing on the didSelectRowAt in which I hope when we tap on the cell, it will store the item into the favoriteList.

Take note that I also use Array(Set(self.favoriteList)) and the reason behind this is to avoid duplication of data. For example, if you press the same data twice, by it’s nature flow, you will have both the same data. But with Array(Set(self.favoriteList)), it will remove duplicate data.

And of course once we clicked it, we will show an alert notifying the user letting them know that the data is stored in the favoriteList.

Next, we will need to pass our favoriteList to our FavoriteTableViewController.

In the FavoriteTableViewController, I have created a variable called favoriteList and it is currently empty and the data is passed through the above step.

By now, all your favorites are being saved and should show up on the FavoriteTableViewController. But how do we remove from our FavoriteTableViewController?

REMOVE DATA FROM FAVORITE LIST AKA PASSING DATA BACKWARD

To achieve this, in FavoriteTableViewController, I’ve created a protocol called FavoriteDelegate on the top of the class. Essentially what it does is add or remove.

Now we will have to inherit the FavoriteDelegate in the following step.

You’ll notice that there are two remove being used here. Because we have two array of favoriteList and each of them are in different class, we’ll need to remove it on both of the favoriteList.

If we do not remove the data from both favoriteList, and let’s say we only remove only one at our FavoriteTableViewController. Each time we go into our FavoriteTableViewController, we’ll see the same data password from MainTableViewController even though we have remove it.

Now in order for this to work, we have to go to MainTableViewController and add the following line of code. Now, we are using the FavoriteDelegate and we will have to inherit the function within. From here, we could modify the function our own need. In my case, I would like it to either remove or add into the array. Lastly, the most important step is to create a variable for FavoriteDelegate and set it to self in viewDidLoad.

Once we have all those down, add one more additional line in the following function where we connect the delegate on both classes.

Your project should then look like the one below. Feel free to check out the GitHub too.

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

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

*
*