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

4.5 Adding an Unwind Segue

We now have a way to enter in some data, or allow the user to enter in some data, about the particular feed they want. But now we need to be able to get from this pop over view controller, this little data entry form back to our feeds view controller. Now this is a li, a little bit interesting because we're, we're talking about a navigation controller. Which is going to go down a path typically from the left to right. So we're gonna go down a path but in order to get back we need to do something called unwind. We want to unwind this segue so that we go backwards a step. And, of course, that's gonna be programmatically based on the selection of some sort of control. In this case our add button. On our view controller. So, now if you've ever paid attention to kind of what's going on once you select one of these view controllers within the storyboard, you have here your actual view controller. So you can select this and look at its properties. You have the first responder, which is going to allow you to. Get to some access to some kind of events that are happening within your UI especially when you're talking about working with your keyboards which we're gonna see in a few moments and then you have this thing here called exit. Now exit is what's going to allow to actually unwind part of your storyboard or actually to exit the entire application should you want to. So, when you're in a story board, when you're kind of using a navigational structure, like we are here, this is going to allow you to unwind the segue, so what you should typically be able to do is select whatever it is you're going to use to initiate that unwind. Control click and drag up to it, and be able to let go and choose where it is you want to go. But unfortunately. By default, it doesn't work that way, so you have to do a little bit of setup in order for this to work. And where we actually want to go is back to our feeds view controller here and, so, once we actually add some. Functionality to this feeds view controller. Then, all of the sudden, our little exit strategy here where we are clicking on the add button is going to work. But, we have to do a little bit of glue here first. So, similarly to when we were working with the add feed view controller and the web view controller, we created something called an outlet or an IB Outlet. Which ultimately is how you get access to certain controls under UI from your code. Now there's another little interesting piece of functionality in here that we're going to use now which is going to allow us to know when a particular. Action occurs, and that is called an IB action. So what we're gonna do is come into our code file here for our feeds table view controller, and we'll come down here maybe just below the didn't receive memory warning. And what we want to do is very similarly to how the IB Outlet looks, we're going to create something with an at symbol, IB, action. Now this is going to create an action, which is going to allow us to actually tie our unwind action from our add feed view controller back to our feeds view controller. So this is just like creating any. Normal functions will create a function, and we'll call this retrieve new feed. So now we have an action, now it's not necessarily doing anything at the moment, but we'll get back to that. So, once we do that, once we create that IB action, you see we get this little empty circle here very similarly to what the IB outlets look like in the previous lessons. So now that we have this, well, what we should be able to do is come up here to our view controller, select our ad, control, click and drag up here. But it's not working yet. That's because it has. To be a very special IB action. So if I come back over here to my fees view controller, it's not just enough to have an IB action. You actually have to have an IB action that has a parameter of a segue. So if we come in here and create a segue and say that this is a UI storyboard segue and save that now. We've created the magic that's going to allow us to create this unwind. So if I hit add, and control click and drag up to exit, I can now select this, and it's going throughout my project to find where we have a segue or an action, an IB action that has a UI storyboard segue to allow me to go back to. So now I can click that. And so if I were to save and build my project again now, and go ahead and run it. What you're gonna see now is if I hit my add button, I come to my feed URL, and if I hit add, I unwind back to where I was. So that's very nice. Now we have, the mechanism to go to a data form and come back from it, but now we actually have to get the data from it. Well, luckily for us. In order to do that, its very similar to what we've done in the past, so we come in here and we take a look in our add feed view controller and once again we have here a segue, our prepare for segue and that's exactly what we're gonna take advantage of, just like we've done in the past. But in order to do that, we have to provide a place to add some data or to do some things over here on our feeds view controller. So in this case, what we're going to do is we're going to restructure our application here a little bit. So to this point, we've just been on our view did load function. We've been. Hard coding some data to go ahead and populate our view, our table view, so actually I'm going to cut this out of here, so we're going to cut that, then we're going to create a new function, function, and we're going to call this Add New Feed. And from in here we're going to just pass in a URL. So we'll call this URL string, and then we'll go ahead and paste our code into here. Now to this point we're not going to hard code this URL anymore, so we're just going to cut this out, and we're simply going to pass it in like this. So go ahead and save that, and we'll build, and everything should go ahead and continue to work. Now, if I run this, we're not gonna, by default, get any of our feeds in here anymore, but we can still create our pop-up. We'll type something in here, hit add, if we wanna populate this. But just to test if everything continues to work, we can come into our View Did Load, and we're just gonna call this add new feed method. And we're going to pass in this URL that we've been using to this point. So go ahead and run our application, and now we should see something that's very similar to what we had before, and we do. Alright, so now in the next lesson, we're actually going to finish wiring up this transition and this segue to actually use some of this functionality that we've just created.

Back to the top