In this section, we'll implement the functionality for users to view and edit existing notes.
First, we'll start by adding a note property to our DisplayNoteViewController. We'll need this to pass an existing note from the ListNotesTableViewController to the DisplayNoteViewController to be displayed.
In DisplayNoteViewController, add the following property at the top of your view controller:
We set our note to have a type of optional Note, or Note?, because it's value can potentially be nil. There are two cases for our note property:
When displaying or view an existing note, our note property will be a non-nil value.
When creating a new note, our note property will be nil.
Passing Note Data Via Segues
Previously, we used prepare(for:sender:) to pass a newly created note from DisplayNoteViewController back to our ListNotesTableViewController.
To display or edit an existing note, we'll need to do the same thing in the opposite direction. We'll need to pass an existing note in the ListNotesTableViewController to our DisplayNoteViewController.
Let's implement this code now.
In ListNotesTableViewController, update prepare(for:sender:) with the following code:
Get a reference to the index path of the selected row in the table view using the UITableView property named indexPathForSelectedRow. We'll use this next to retrieve the correct note in our notes array.
Retrieve the selected note using the index path from the previous step.
Get a reference and type cast our segue's destination view controller. This will allow us to set the note property of the DisplayNoteViewController in the next step.
Using the reference to destination view controller, set the note property to the selected note.
Displaying The Note
With our note data successfully passed between view controllers, we can display the existing note.
Previously in our DisplayNoteViewController, we added code in our viewWillAppear() to remove our storyboard's filler text. To display our existing note, we'll need to update this method to populate the text field and text view (if the note already exists!)
In DisplayNoteViewController, update viewWillAppear() with the following code:
We've added quite a bit of new code, let's break this down:
We add a if-let new clause to our guard statement to check that the destination view controller of our segue is of type ListNotesTableViewController. This allows us to safely type case our segue's destination view controller and access it for each case of our switch-statement.
We split our save case for our switch-statement into two. The first for if we're saving an existing note. We're able to know the note already exist because we check the notes property of our DisplayNoteViewController for a non-nil value. If the note exists, we update it's properties and reload the table view of our ListNotesTableViewController.
If the note property is nil, we know the user is creating a new note. In which case, we keep the previous code for creating a new note.
Notice that since we type cast our destination view controller in our guard-statement, we can use it directly without more downcasting.
In the case of saving an existing note, we don't have to pass the modified note back to our ListNotesTableViewController. This is because in Swift, class types are passed by reference.
This means that both view controllers reference the same class instance. If the class instance is modified in one view controller, the other view controller will also access the same modified object.
We do, however, need to make sure the table view knows to reload it's cells.
Running the App
In this section, we implemented the functionality to view and edit existing notes. Once again, let's make sure everything works!
Build and run the app. Make sure you can create new notes and edit existing ones.
In the next section, we'll look at how to implement the functionality for deleting existing notes.