2.4 Creating ViewController Subclasses
Now that you have most of the Storyboard for your application created, we need to address a very important issue. How are you going to get access to the different controls on the Views from your code? Eventually you will need to get or change a property here and there. Subclasses are the answer, and they’re exactly what we will look at in this lesson.
1.Introduction3 lessons, 06:16
2.Setting Up the Project5 lessons, 1:00:13
3.Processing Data3 lessons, 37:52
4.Interacting With the UI6 lessons, 41:27
5.Core Data6 lessons, 44:36
6.Conclusion1 lesson, 02:47
2.4 Creating ViewController Subclasses
Okay. So, now if you've been following along to this point, you know in the previous lesson we set up the basic high level UI flow of our application, using our storyboard. And we have three basic screens here, all represented by view controllers. And then we have this navigation controller that sits out to the left, that's actually going to drive the flow or the navigation features of our application. So, what I wanna do in this lesson is take a little moment to dig into one of these view controllers, a little bit more specifically. So, you can see how we're going to begin to add functionality or get the availability of adding functionality via code to these actual view controllers. So, if you've never played around with storyboards before, this would be very beneficial. But if you have, this should be fairly old hat. So, what I wanna do is I wanna look specifically at this root view controller that we're gonna use for other feeds. So, I'm gonna zoom in a bit here. Let's take a closer look at our ro, root view controller here. So, what I wanna be able to do ultimately is get access to the table view that's within our view controller here as well as the prototype cells and any other aspects that I'm gonna need to in here. So that I know not only when I want to navigate out of this view controller, but what I need to populate the cells within this particular view controller or within this table view. How I can get access to those programmatically because the storyboard is nice. It's a drag and drop kind of whizzy-whig feel. But ultimately, I have to provide some sort of functionality to this. And so I'm gonna show you in this lesson, step by step, how you go about dong that. And then, we'll go ahead and fly through the other ones on the next lesson. So what we want to do in this case is, I want to be able to create a class or create a, some sort of mechanism that's going to allow me to get hold of all the aspects of this particular view controller. And the way that we do that is by doing something called sub-classing this particular view controller. So, what I'm gonna do is I'm going to come over to my project. And so I'm gonna select anywhere in here really. So, let's select the SwiftReader folder. And then we'll go ahead and select file or use the right-click and choose New File. And so what I wanna do in this case is I wanna make sure I'm in the iOS family and select Source. I want to create a cocoa touch class. So, I'm gonna select next, and now you can give a name to this class. In this case, it's gonna be some type of view controller, and then what type of subclass it is. So, let's take a look at this really quick. So, I want to create a class that's going to represent my feeds' view controller, so why not call it that? So, we'll call this FeedsViewController. Now, we're gonna ca, make this a subclass of something. Now, the rule of thumb is if you want to create a subclass that's going to map to the functionality and allow you to programmatically get access to things you've built out on the storyboard, especially at the view controller level. You wanna select the most specific version of that view controller class that you can, so that you can accurately and easily apply this subclass. So, in order to do that, we're gonna need to select the UI table view controller because that's what that particular controller is, that root view controller that's gonna hold all of the feeds within our application. Now you have an option here to also create a XIB file. You don't need to do this because the actual user interface is already gonna be generated for us via the storyboard. But if you weren't using storyboards, you could have it automatically create a XIB file, which really just another file. It's kind of like an XML format that's gonna describe a UI that you can play around with within interface builder in Xcode. But we don't need to worry about that right now. As you can see here, we're by default given this to be an iPhone target. And then, we can select the language, which we're gonna stick with Swift. So, we go ahead and hit Next. And as you can see, it's also gonna ask us where to put it. So, we'll stick it in the SwiftReader folder. And then we'll select Create. So, now we're given this kinda boilerplate code that's gonna represent a view controller. As you can see here, the FeedsTableViewController that we created, and it's also a UITableViewController. So this is what happened when we said we wanted to be a subclass of a table view controller. Now, within here, we have a bunch of overrides of different methods or events you can almost say within this particular view controller that we can start to tie into. And we're definitely going to do that as we start to add some functionality to our application. But for now, as you can see, we have viewDidLoad, which means that as soon as this view or this view controller presents its view, this particular method is going to fire. And then, didReceiveMemoryWarning, this would mean that while you're in this view, if your application begins to take up too much memory, if there's not enough memory left on the actual platform, you could receive this warning. So, you can start to get rid of any heavy resources if you needed to. And any number, number of other things. Now we also have two overrides here, as you can see, by default. The number of sections in the table view as well as the number of rows in the selection or in the section. Now, these are specific to a UITableView controller. And we're gonna tie into these as we start to add functionality to this. But just know that when you're using a table view controller, you can break up these cells within that particular view controller into a number of different sections. So, in this case, we're not gonna worry about it. So, I can already tell you that this is ultimately gonna be one. Because we're not gonna break these up into sections. It's all going to be in one long list. And then the number of rows and sections, this is gonna be very dependent on how many, at least for this view controller, how many feeds we have loaded into this table. Then we're also going to need this third one that is commented out for the moment to be able to get access to the cells, the individual cells within our table view controller. So we can do things like set their text or handle clicks or other things like that. So, that's really all we need to know about this particular pre-generated code. And it's all gonna look very similar to this every time you generate a class that's gonna represent a view controller. And you're gonna see that in a couple other lessons as well. So now that we have this class, this view controller, what are we gonna do with it? Well, we can come back to our storyboard now. And if I were to make sure that I have this view controller selected, and I open up my right pane here. And then I go into my Identity inspector. You're gonna see here that we have an option to set a custom class. And I can specify the name of this particular class, and if I were to hit this little arrow, you're gonna see I have two options. I have a UITableViewController, which is the default that contains all of the default functionality that you're ever gonna need for a base, out of the box UI table view controller. And it also noticed that I have created my own view controller that is a subclass of a UI table view controller, namely the FeedsTableViewController. So, now I can select that. Now, through the magic of Xcode, now every single time that this particular view is shown, this UI table view here for our feeds, it's going to know that I am using my view controller, my FeedsTableViewController. And it's gonna use this class, specifically, to add the functionality to that particular screen. And now that we have our subclass set for this particular view controller, now we can prove that our FeedsTableViewController is actually assigned to this particular view controller every time it loads. So, let's go into our FeedsTableViewController. And just to prove a simple point, nothing that's gonna be overly amazing at this point. But just to show you that we actually have access to what's going on now, I'm gonna go into my viewDidLoad method. And we'll come down under the call to the super.viewDidLoad method and we're just going to log out of line. So, we're gonna say, NSLog and we can put anything want on here. I can say, hello from FeedsTableViewController. Something like that, nothing, nothing overly complicated but just to very simply prove a point. So, we'll go ahead and save that. So, now we're gonna go ahead and run our application just like we've done before and make sure that we've got everything correct here. And so now when our application runs or when the iOS simulator fires up, you're gonna see that we have our feeds table here. But if you also take a look down in the bottom, kind of in our little console output view, you can see, hello from FeedsTableViewController. So, that just goes to show you that now, when this particular view shows up, I now have access to run code anywhere within this particular class and have it adjust things or populate things in here. More specifically, show the feeds when things actually show up and even log things on the output if we need to. So, that's the very basic premise of what it takes to actually create a subclass, and then assign it to one of our storyboard views or view controllers. So, from this point, I can start to wire up buttons or things like that, that would ultimately give me access to write code against that and maybe do some sort of operation. Unfortunately, I don't have any buttons in this particular view yet, but we're gonna add one in very soon. So, let's just go ahead and finish up this process. And I'll go ahead and pause the video now. And when I'm done, I'll come back and show you that I've, I've created subclasses for all of our different view controllers. All right, so, here we are back again within our application. And if you pause, if you paused the video and kind of worked along with me, let's go ahead and see what's happened so far. So, if you recall we have this concept of an articles view controller, but at the time it was just a table view controller. So, what I have gone ahead and done is I've created this class, this ArticlesTableViewController the same way. It's sub classing the UITableViewController. And then within our storyboard, I've made sure that the custom class on the Identity inspector is once again, set to the proper class, which happens to be ArticlesTableViewController. And then just slightly differently, I've come over here to my third view controller, which is not a table view controller. It's just a plain old view controller. And I've created a WebViewController. As you can see, the implementation of this is quite a bit different. It's much smaller. It only has two methods and actually three, here on the bottom, which we're going to be going over in another lesson. But we've lost all of that table view specific functionality, which is okay cuz we don't need it for this particular view controller. So, I've gone into my storyboard. I've made sure I've selected my web view controller, and cr, selected its custom class to be a WebViewController. So, there you have it. We have now created subclasses for all of our view controllers that we need to get ahold of something in programmatically, namely the feeds and the articles and the actual article web view itself. So, now, in the next lesson, we're gonna take a look at a couple more things that have to do with this concept of subclassing in a somewhat similar way. Where we get to play around with some of the properties associated with pieces or bits and pieces of our view controllers and the transitions. So that we can get access to them programmatically. So, let's go ahead and see how that works.