Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Swift
  • Overview
  • Transcript

2.5 Passing Data via Segues

As we covered in a previous lesson, the transitions between screens within the Storyboard are referred to as segues. Well segues are more than just a pretty face. We can also use them to pass data from one ViewController to the next. Let's see how that works.

2.5 Passing Data via Segues

So now, moving right along within our application. We're going to play around a little bit more with another aspect that's going to become very important within our storyboard and within our view controllers and that's specifically these transitions. So these transitions or these segues down here, if I were to zoom into 100%, I can select this particular segue here, and we'll see what's originating it. So when we click on this particular cell, it's gonna transition us, or give us a segue into the Articles view controller from the Feeds. So if I were to open this up a little bit, and go ahead and take a look at our Attributes Inspector here. You're gonna see if I select this particular segue, I'm given an option of, of course, modifying or changing what type of segue this is. But I'm going to leave it as a show. But I also have this concept of an identifier. So what is that? Well, you can provide an identifier for this particular segue so that you can get access to it via the code that's in our subclass. So let's go ahead and give that a try because this is gonna become rather important to you when you begin to create more complicated UIs. And, say, have separate possibilities or separate multiple segues transitioning to a number of different views or view controllers. So we're gonna give this an identifier and you can really give it anything you want, but we'll just call this, ShowArticles, just as a simple identifier. So now what we're going to do is I'm going to show you how the process of transitioning from one view controller through a segue to another view controller, how that looks like in code. So let's go ahead and give ourselves a little bit more real estate, and now we want to head back to our feed's table view controller. So now we can dump this NSLog statement here. And we'll come down here and we're gonna take a look at some of the commented out code. And if you go down to the very bottom, you're gonna see this mark navigation. And we have a commented out function here that is called prepareForSegue so every single time within your application before. Before a segue fires and before a transition happens, you have access to do some magic or do some code that's going to allow you to handle something before that transition takes place. And one thing that you're going to find that you're gonna do within this particular course. But in several other applications in your lifetime, is gonna be transferring data from one view or from one view controller to another one via a segue. So it's not a very difficult thing to do but what you're going to need to be able to get access to is this segue parameter. So if I were to come in here and take a look at segue, you're gonna see that there's a an identifier. So ultimately, or right away, I have access to check to make sure if this particular segue is the proper one that I want. Because, no matter how many transitions that you have or segues that you have defined, from one view controller to a slew of others, you only get one prepareForSegue method. And that's where that identifier becomes very important. So let's just, out of good practice, start to take a look at this. We're going to say all right, well, if my segue.identifier is equal to what I specified out there as ShowArticles. Then we can go ahead and perform some sort of action or some, something that we want to do before the segue actually fires. So another interesting, or two more interesting things, about this segue parameter. Is I have what's known as the sourceViewController, which is going to be where I'm coming from or ultimately, where I am right now which is the feedsTableViewController. And I also have a destinationViewController, which is going to give me access to where it is I'm going. So let's play around with this because this is gonna become very important as we start to add specific feed reader functionality to our application. So, what I want to do is I want to be able to transport some data from feeds view controller over to the articles view controller. But before we can actually do that, we need a way to store this data in our trans, in our destinationViewController, which happens to be articles in this example. So, what I'm gonna do is I'm gonna come down into my class and I'm just gonna create a simple variable. We're gonna call this temp and it is going to be of type string, and we're going to initialize it to an empty string. Now, if you're unfamiliar, once again, with the Swift syntax and how all this stuff works. All this really is, is var keyword is going to specify that I want to create a variable, not a constant, but a variable. With a name of temp, and then we give it a se, a colon, and we give it the type of, the type of the particular variable, and then we can initialize it to something. So ultimately, in this case, I don't need to say explicitly what the type is, because it will use type inference to figure it out. But, usually, when I'm kind of getting started out with something like this, I like to be as explicit as I can, so that I can see everything that's going on, and have it just be right in my face. This could be beneficial for you, but it's also beneficial for me as I'm going along and so we don't get confused. So now, within the articles table view controller, I have a temp variable. So we'll go ahead and save that. We'll head back over to the feeds table view controller. And now, what I can do is I can get access to where it is I'm going, which is ultimately an articles table view controller, and I can do something with that temp variable. So let's go ahead and get access to the articles table view controller. So we'll call this, we'll create a variable, and we'll call this the articlesController, and then we're gonna set that equal to, within our segue, we want to go to our destination view controller. Now, in this case, this is just an any object, and if you saw that before here, I'll, I'll bring that back to you. The destination view controller is just of type any object which is just basically a generic type to say that basically, anything can fit in there. It's kinda one of those catchall buckets. So this isn't gonna help us. What we need to do is we need to cast this into what the actual type is that we're going to. In this case, we know that it's gonna be an articles view controller so I can use the as keyword to say I want to cast that destination view controller as an articles view controller. So now, I can say, all right, within my articles view controller or my articlesController variable. I now have access to that temp variable so I can say, hello there. And once I've done that, I don't have to do anything else, now I have specified that I have created some sort of value, I've assigned it to that tenp variable on that destination, and now it's just ready to fire. So what are we going to do with that though because we're not actually doing anything with that temp variable on the article side? Let's come back over here and we'll just do another simple log statement. So we'll do NSLog. And then when we fire on this viewDidLoad, we just want to print out whatever is in the temp variable. So, we'll go ahead and do that. So, we'll go ahead and save and then we'll build just to make sure everything is correct. And it is. And now if I were to run my application, my feeds view controller shows up. But as you can see here, I don't have any way to fire that particular segue, cuz all I have is my empty, table view. So now we need to add something in here that's going to allow us to actually kick off that segue. So this is a nice, another little [LAUGH] segue of my own, excuse the pun, to add in a button that we're going to need later on. So we'll go ahead and we'll stop this and we will come back over to our feeds table view controller and ultimately into our storyboard. So if we come back here and take a look at our feeds table view controller. If I were to select this view controller, as you can see in the middle here, I have the title where it says feeds but there's a lot of blank space on the other sides. Well, the navigation controller is going to fill that in with some other things depending on how far down the navigation view you get. So if you click on a title or click on a feed and it takes you to the articles, up here on the left-hand side, you're going to get a back button. It's not going to say back, it's gonna use the title of the previous view controller, but on the right-hand side, that still leaves a little bit of real estate to be able to put some functionality. So what I wanna do is I wanna come down here to the bottom and I wanna look at all the different widgets and things that I can put in here. And what I am looking for at this point is going to be a bar button item. So if I scroll down just a little bit, you are going to see that you get the option of adding a bar button item. So I am going to drag this onto this bar up here where it's going to say, item. And so if I were to select this guy and come over here to the title. I can get rid of this title and I can come up and I can select the identifier. And I get a bunch of pre-built, kind of, niceties here so I'm going to select Add because that's going to give me the plus button, but you can really choose anything you want. You can select Edit. You could select Compose, and you're gonna get all these nice little icons that come built in with iOS, but in our case, what we're ultimately gonna need is this Add button. So it's gonna give this plus sign, so that we can tie into the functionality of being able to add new feeds. So that's where that plus button is going to come from. So, I am going to cheat just a little bit here, just so you can see this segue fire. But what I am going to do is I am going to select this plus button and I am going to, once again, Ctrl+drag over to the articles and select Show. So now, as you can see here, we have two, so if we select one, I have zero identifier, I have no identifier here, but if I select the other one, I get ShowArticles. So in this case, I'm just gonna create another name here so we'll just call this SomeSegue, just to differentiate and we'll go ahead and save that. So we're just gonna come back into our Feeds table, now this is kinda throwaway code but it's just to show you something. So, I changed this to ShowSomething, or SomeSegue. So I changed this to SomeSegue. So we'll go ahead and save that. So now when I click that plus button, I'm going to get this prepareForSegue method to fire. It's going to check to make sure that it's this particular segue. It's going to grab that article's view controller. It's going to set that temp variable to, hello there, and then, if I come over here, it's just gonna log that out. So, we'll go ahead and save and build that just to be sure. And now, we'll go ahead and run our application and, once again, and the simulator will show up and now I get this nice little plus button here. So, we'll go ahead and hit the plus. Now, we transitioned over to articles and, as you can see on the top-left, we can go back to where we came from. And, the title was Feeds, but, you also can see that I have Hello there logged out to the console to the output window down here. So, that's how you have the opportunity to pass data from one view controller to another via the segues. So I can select Feeds. It'll take me back, but we don't want this to transition over to articles. This is what's gonna ultimately use a pop-up or a pop over view controller to ask the user what it is they want to add or what feed it is they want to add. But this was just to show a purpose. We're gonna stop this here, and then we're gonna get rid of some of this throwaway code just so that we're left with a clean slate when we actually have to write the code to run this. So we'll come back to Feeds and we'll make sure that we get rid of all this. So now you can see whenever you want to transition or have some sort of access to that segue, that transition before it happens, you're going to use the prepareForSegue. Now, make sure to come into storyboard and I can get rid. So I can get rid of that actual segue and so now we're back to where it was before. But now we have this nice little bar button item that we'll be able to use later on when we actually start to add feeds into our application.

Back to the top