Pass Data Uistoryboard Segue Xcode Swift Ios

Pass data using UIStoryboard segue between UIViewControllers

I’ve been asked a lot of times, how to achieve the most common scenario in iOS, that is to pass data with UIStoryboard segue between 2 UIViewControllers. Most common because it happens almost everytime you interact with an app. Think about Instagram for example, when you tap on a post(image, video) you perform a push segue, pushing a new UIViewController on top of the current one, and that controller displays the object you selected.

This is a must have if you want to become a good iOS developer.

What we need for this?

For this tutorial, as our base project, we are going to use a previous tutorial’s code where we showed you how to evenly space out UIViews with just using Auto Layout.

pass data with UIStoryboard segue

Once you downloaded that project, we can jump right into it.

Some prework(out)

Let’s start with the object that I actually showed you already. Add a new class and call it Post, subclass it from NSObject and add to properties.

create post object to pass data using UIStoryboard segue viewcontroller xcode swift

It would look like this:

As you can see, nothing fancy is going on here. We have a Post class that has a name and an imageName property. I’m just using local data right now, next time will show you how to do all this with remote files. (Downloading objects from and display them properly)

Next, go over to the ViewController class and create an array, that will hold the objects. Let’s add a new variable and call it arrayOfPosts.

This array will hold all of our Posts and display them in the collectionView. Add this method to viewDidLoad().

Obviously we also need to modify the collectionView’s datasource methods, so that it will know where to load the Post objects from. First adjust the numberOfItemsInSection function:

Than the cellForItemAtIndexPath, to load the proper post object at the given indexPath:

Once we have that, we are ready to get started with the real deal.

Pass data using UIStoryboard segue

There are couple of things we need to do before we can actually pass data properly between two viewControllers with a segue.

Our viewcontroller need to implement the prepareForSegue function, to be able to interact with the segue right before it is performed. What segue you might ask? Well the one that is called when you tap on a collectionViewCell.

pass data using UIStoryboard segue viewcontroller ios xcode swift

In this method, we check if the segue’s identifier is the one we just specified in Interface Builder and if so, than get the right indexPath of the selected cell and pass that object to the destinationController. Sounds easy right?

Let me break it down for you.

Each segue object has the following properties:

  • identifier: what you specify in Interface Builder
  • sourceViewController: where the segue was being called from
  • destinationViewController: which viewController will be presented

And you see another object, called: “sender: AnyObject?” which could be anything, that triggered the segue. In our case this is the cell itself.
So all we need to do, is check if the sender object’s class is equal to our collectionViewCell (ImageCollectionViewCell), if true, than get the indexPath for that cell and with that indexPath we can grab the selected item. Than, once you get the proper Post object, get the destinationController object from the segue, and assign the selected Post object to it.

(to be able to do this, you need to add a new variable to your PostDetailViewController, like so: var selectedPost: Post!)

As you can see it is pretty easy to pass any kind of data between 2 viewControllers.

Hope you found it useful, download the project below.

  • iosMaher

    thx so much , i was looking for this project for while