Now, when you look at most to-do list apps out there, it's very rare that you'll come across a to-do list 1
that only has a single list. But, unfortunately, that's currently all that we've got. Most to-do list apps 2
will have categories. 3
So, for example, cleaning, bills, miscellaneous, grocery, and you'll be able to tap on them to view those 4
lists, 5
and this allows us to classify and categorize so that we can organize our to-do list items in a more 6
logical way. 7
So that's also a good feature for us to add to our Todoey app. And in order to implement it, 8
we'll get to learn all about relationships and how we set them up using Core Data. 9
So the first thing I'm going to do is I'm going to make some UI changes. At the moment, 10
we've got a single table view controller that is embedded inside a navigation controller. 11
Now, in order for us to have a category screen, then we need to drag on another table view controller. 12
So I'm going to place that between the navigation controller and our to-do list items table view controller. 13
Then I'm going to delete this link between the navigation controller and the to-do List controller because 14
this link specifies which is the 15
root view controller, i.e., if you have a navigation stack like a stack of cards, what is the controller that's 16
at the bottom of the stack? And when you say something like pop to root view controller, then that's equivalent to 17
the first screen that's embedded in the navigation stack. 18
So let's go ahead and click on this link and delete it. And then I'm going to replace this category view 19
controller into a navigation controller and our to-do list view controller, and then I'm going to click 20
on the navigation controller, 21
so I get this little yellow button here, I'm going to control drag it to point it towards our brand-new 22
table view controller. And instead of using show or creating a segue, instead, I'm going to create a relationship 23
segue and specify that this is now the root view controller because we first have to see the category 24
table view, 25
and only then can we select one of the categories to take us to all the items that are classified under 26
that category. 27
So that means we also need to set up a segue between our category and our to-do list items by control 28
dragging. 29
And in this case, we're going to go for a show segue. 30
And if you remember every single time we set up a segue, we also have to specify an identifier. 31
So we'll just call this goToItems. And we currently now have this free-floating table view because 32
it doesn't have a view controller class that's associated with it. 33
So let's go ahead and create a new one. And instead of creating a new Swift File, we want a new Cocoa 34
Touch Class. 35
So hit next. 36
And this is going to be a subclass of UITableViewController because we want all of that freebie 37
code that comes with it. 38
And then we're going to call this class the CategoryViewController, make sure language is selected to the Swift, 39
and hit Next. 40
And then we're going to save it inside the Controllers folder or group and making sure that it targets 41
our Todoey app, and then we're going to get Create, and we now have a CategoryViewController. So, I'm actually 42
going to drag that on top so that it makes more sense because we first go to Category, then we go to the 43
to-do list for each category. 44
So, now if we go back to our Main.storyboard, we can specify the Swift file that is going to be responsible 45
for managing this particular view. 46
So if you don't do this step and you head over to the assistant editor, when you select the to-do list 47
item view control, you see the under automatic, 48
we've got TodoListViewController.swift. 49
But when you select this new table you control that currently is not associated with any Swift class, 50
then you can see that under Automatic, you only get a UITableViewcontroller file, and this is also not 51
a Swift file. 52
This is actually an Objective-C header file. 53
So that's not what we want because we can't link up our outlets and we can't do anything, 54
so we have to specify the identity of this 55
view controller. And inside the custom class, we're going to say 56
CategoryViewController and hit enter. 57
So, now automatically, our assistant editor has updated to the corresponding class with our view controller. 58
Before we leave the Main.storyboard is we want to give this prototype cell a identifier. So select the 59
prototype cell, either on the canvas or select in the document outline, 60
but we're going to call it CategoryCell, and hit enter. 61
So, now that we've got our view and our class linked up, we can now create a add button or Bar Button Item 62
rather, 63
and we can drag it to the top right corner of this new 64
view controller. 65
And, again, this is going to be a system item of Add and we're going to change the tint to white. 66
This is going to allow us to add new categories to this table view so that we can organize our to-do list. 67
And once you've done that, we're going to control drag over to our CategoryViewController.swift file 68
and we're going to create a new IBAction, and this IBAction is going to be called add ButtonPressed 69
and it's going to be of type UIBarButtonItem, and hit enter. 70
And the last thing I'm going to do here is select the navigation item here and I'm going to give it 71
a title called Todoey, and then I'm going to change the title here to Items, and that is all we need to 72
do 73
inside the Main.storyboard. 74
So let's head over to the CategoryViewController. And the first thing I want to do is when you set up 75
a new Cocoa Touch Class, that subclasses UITableView, you get a whole bunch of free code, but you also 76
get a lot of comments. 77
And you know we're now pro enough with table views to not need all of this help and guidance. 78
So I'm going to delete all of this code up until viewDidLoad, and then I'm also going to delete all 79
the comments inside viewDidLoad. That cleans up. 80
view controller a little bit, and allows us to work on it without all of this distraction. 81
So, now that we've set up our view and our controller, we're going to go and figure out what we're going 82
to do with our data model. 83
So up till now, we've been editing our entities and attributes under this table view format, but you can also 84
switch to the graph view and this presents each entity and all of the attributes as a little table. 85
And for those guys who've been working with databases, then this might seem quite familiar to you. 86
So it's under this format that we're going to add a new entity, and this new entity is going to be called 87
Category. 88
And once you hit enter, then you'll see this new table show up 89
that's called Category which has no attributes and no relationship. 90
If you select the Category entity and click Add Attribute, then you're going to get a new attribute. 91
And we can click on it to modify its properties inside the Data Model Inspectah in the right-hand pane 92
here. 93
Now, for some reason I have no idea why the icon for it is like a little welding mask, but some designer 94
somewhere felt that this was a good idea to represent a data model. But, hey, let's not argue with Apple 95
designers, right? 96
Okay, so we're going to change the attribute name to be called name. 97
And this is going to refer to the name of each category that we're going to add, and we're going to make 98
the name nonoptional. 99
So it has to have a value for it to go into our data model. And you'll see this little error here and it won't 100
go away until you change the attribute type from Undefined. 101
It cannot be undefined. 102
It must be specified. And our name for the category is, of course, going to be a string. 103
And once you do that, then Xcode is all happy with you, and all the errors go away. 104
Now, in addition to having a single attribute for the category, we're also now going to set up a relationship 105
between our category and our item entities. 106
And to do that, all you need to do is control drag from Category to Item, and now you have a relationship. 107
Now, it's not very useful having a relationship called new Relationship. 108
So we need to rename that. 109
So click on it and that will allow us to edit it in the Data Model Inspector. We're going to change the 110
name to something that makes sense. 111
So each category can be associated with many items, right? 112
If you have a category called shopping list, then you might have 10 item objects that refer to all the 113
things you need to buy. 114
So we can call this relationship Items because that's essentially what it's referring to. 115
And we should change the type from To One to To Many because each category can have many items associated 116
with it. 117
Now, the inverse relationship refers to what you call the Category that the Item is associated with, and 118
that should probably be called the parentCategory, and each item can only have one parentCategory. 119
So this type should remain as To One. It will be really confusing if each of your items have two categories, 120
say, you know, your "buy milk" belongs in shopping list, as well as another category. That doesn't make 121
any sense, logically. 122
So we've now created a new entity called Category and we've also started creating our graph which specifies 123
how categories relate to Items. And this might be a good time to remind yourself of when we first started 124
talking about databases in this module, when we are talking about our bakery having a product's table, 125
a buyer's table, and an order's table. And in order to manage all of the complex data, we need to set up 126
these relationships. 127
So, now when we create a new Category, we can link it with many Item and as managed objects. And every 128
single item object, we can look up its parent category. 129
So this now gives us access to more complex things that we can do with our data including setting up 130
this new view controller and having a whole bunch of categories that each have their individual to-do lists. 131
Now, all we need to do is head back to our CategoryViewController and we need to set up some code to 132
use our data model. 133
So in order to do that, we'll need to import Core Data and we'll also need to have some TableView 134
DataSource methods, as well as TableView Delegate Methods, and then we'll need some Data Manipulation Methods. 135
So I think you're probably getting the hint. 136
I'm not going to do this for you. 137
I want you, as a challenge, to think back to what we did inside our TodoList View Controller, how we 138
set up an array of items, how we tapped into the context that we're going to use, and then how we set 139
up our tableView to load our data from the context for all of those items. And then we used our 140
IBAction addButtonPressed to add new items into our tableView. 141
And then we used the saveItems function to persist the items inside our container, and then we loaded 142
those items from our persistent container using our context. 143
Now, I want you to complete this challenge and be able to set up our CategoryViewController in a similar 144
way to what we did with items to use our Core Data data model and to be able to add new categories and 145
to be able to show those categories inside our table. 146
So all you need to do is set up the data source so that we can display all the categories that are inside 147
our persistent container. 148
Set up the Data Manipulation Methods, namely, save data and load data so that we can use CRUD. 149
And finally, to set up the addButtonPressed IBAction in order to Add New Categories using our category 150
entity. And the part that I want you to leave for now is the TableViewDelegate methods 151
So that refers to what should happen when we click on one of the cells inside the category table. 152
So I know that these challenges are getting more and more challenging. 153
But if you've come this far and you followed along with everything that we've been talking about with 154
Core Data, this is a good time to really test to see if you understood the concepts and use what you learned 155
to set up your very own table view controller that has a persistent data store using Core Data. 156
And if you have completed the challenge successfully, then you should end up with a blank table view 157
with the title Todoey, and you should be able to click on the Add button to add new categories to your 158
table view, 159
and when you double click on the home button and terminate your app. 160
Now, I really, really recommend you to give it a go yourself. And it might require quite a bit of struggling, 161
but struggle is good. 162
And once you struggle through it and get it to work by yourself, then you'll feel this immense sense 163
of achievement. 164
But if you do get stuck and you do have any issues, then you can always head over to the next lesson 165
where I will walk you through how we can do this. 166
So pause the video and give it a go now. 167
I'll see you on the other side.