Students will build a simple Journal app to practice MVC separation, protocols, master-detail interfaces, table views, and persistence.
Journal is an excellent app to practice basic Cocoa Touch princples and design patterns. Students are encouraged to repeat building Journal regularly until the principles and patterns are internalized and the student can build Journal without a guide.
Students who complete this project independently are able to:
- understand basic model-view-controller design and implementation
- create a custom model object with a memberwise initializer
- understand, create, and use a shared instance
- create a model object controller with create, read, update, delete methods
- implement the Equatable protocol
- implement a master-detail interface
- implement the UITableViewDataSource protocol
- understand and implement the UITextFieldDelegate protocol to dismiss the keyboard
- create relationship segues in Storyboards
- understand, use, and implement the 'updateWith' pattern
- implement 'prepareForSegue' to configure destination view controllers
- add data persistence using NSUserDefaults
- understand, use, and implement the builder methods pattern
- create a custom model object with a failable initializer
- use the array map method to translate objects
Create an Entry
model class that will hold a title, text, and timestamp for each entry.
- Add a new
Entry
class as anNSObject
subclass - Add properties for timestamp, title, and body text
- Add a memberwise initializer that takes parameters for each property
- note: Consider setting a default parameter value for timestamp.
Create a model object controller called EntryController
that will manage adding, reading, updating, and removing entries. We will follow the shared instance design pattern because we want one consistent source of truth for our entry objects that are held on the controller.
- Add a new
EntryController
class as anNSObject
subclass - Add an entries NSArray property
- Create a
- (void)addEntry:(Entry *)entry
method that adds the entry parameter to the entries array - Create a
- (void)removeEntry:(Entry *)entry
method that removes the entry from the entries array- note: Look at the
NSArray
documentation to find how to remove objects
- note: Look at the
- Create a sharedController property as a shared instance.
- note: Review the syntax for creating shared instance properties
+ (EntryController *)sharedInstance {
static EntryController *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [EntryController new];
});
return sharedInstance;
}
- Implement the NSCoding protocol on the Entry class
- Create a Unit test that verifies NSCoding methodality by converting an instance to and from NSData
Build a view that lists all journal entries. You will use a UITableViewController and implement the UITableViewDataSource methods.
The UITableViewController subclass template comes with a lot of boilerplate and commented code. For readability, please remove all unused boilerplate from your code.
You will want this view to reload the table view each time it appears in order to display newly created entries.
- Add a UITableViewController as your root view controller in Main.storyboard and embed it into a UINavigationController
- Create an EntryListTableViewController file as a subclass of UITableViewController and set the class of your root view controller scene
- Implement the UITableViewDataSource methods using the EntryController entries array
- note: Pay attention to your
reuseIdentifier
in the Storyboard scene and your dequeue method call
- note: Pay attention to your
- Set up your cells to display the title of the entry
- Implement the UITableViewDataSource
commitEditingStyle
methods to enable swipe to delete methodality - Add a UIBarButtonItem to the UINavigationBar with the plus symbol
- note: Select 'Add' in the System Item menu from the Identity Inspector to set the button as a plus symbol, these are system bar button items, and include localization and other benefits
Build a view that provides editing and view methodality for a single entry. You will use a UITextField to capture the title, a UITextView to capture the body, a UIButton to save, and a UIButton to clear the title and body text areas.
Your Detail View should follow the 'updateWith' pattern for updating the view elements with the details of a model object. To follow this pattern, the developer adds an 'updateWith' method that takes a model object. The method updates the view with details from the model object.
- Add an 'updateWith' method that takes a model object as a parameter (in this case, an Entry)
- Implement the 'updateWith' methods to update all view elements that reflect details about the model object (in this case, the titleTextField and bodyTextView)
This view needs to serve as a reading and editing view. You will add a UITextField to display the title (titleTextField) and a UITextView to display the body text (bodyTextView), a 'Clear' button that resets both fields, and a 'Save' button that saves the new or changed entry.
- Add an
EntryDetailViewController
file as a subclass of UIViewController - Add a UIViewController scene to Main.storyboard and set the class to
EntryDetailViewController
- Add a UITextField for the entry's title text to the top of the scene, add an outlet to the class file, and set the delegate relationship
- Implement the delegate methods
textFieldShouldReturn
to resign first responder to dismiss the keyboard - Add a UITextView for the entry's body text beneath the title text field, add an outlet to the class file
- Add a UIButton beneath the body text view, add an action to the class file that clears the text in the titleTextField and bodyTextView
- Add a UIBarButtonItem to the UINavigationBar as a Save System Item, add an action to the class file
- Add an Entry property
@property (strong, nonatomic) Entry *entry
- In the action check if the entry property holds an entry, if so, update the properties of the entry. If not, call the save method on the
EntryController
. After saving the entry, dismiss the current view.- note: You may need to add a segue to see a UINavigationBar on the detail view, and a UINavigationItem to add the UIBarButtonItem to the UINavigationBar, this will be covered in the next step
You will add two separate segues from the List View to the Detail View. The segue from the plus button will tell the EntryDetailViewController that it should create a new entry. The segue from a selected cell will tell the EntryDetailViewController that it should display a previously created entry, and save any changes to the same.
- Add a 'show' segue from the Add button to the EntryDetailViewController scene and give the segue an identifier
- note: Consider that this segue will be used to add an entry when naming the identifier
- Add a 'show' segue from the table view cell to the EntryDetailViewController scene and give the segue an identifier
- note: Consider that this segue will be used to edit an entry when naming the identifier
- Add a
prepareForSegue
method to the EntryListTableViewController - Implement the
prepareForSegue
method. Check the identifier of the segue parameter, if the identifier is 'toAddEntry' then we will present the destination view controller without passing an entry - Continue implementing the
prepareForSegue
method. If the identifier is 'toViewEntry' we will pass the selected entry to the DetailViewController by calling our- (void)updateWithEntry:(Entry *)entry
method- note: You will need to capture the selected entry by using the indexPath of the selected cell
- note: Remember that the
- (void)updateWithEntry:(Entry *)entry
method will update the destination view controller with the entry details
- Implement UITableViewCellEditingStyles to enable swipe to delete entries on the List View
- Update Unit and UITests to verify delete methodality
You will use NSUserDefaults to add basic data persistence to the Journal app.
Because Entry
class objects are not plist compatible, and NSUserDefaults
will only store classes that are, we have two options:
- Implement NSCoding to allow native saving and loading from plist objects like NSUserDefaults
- Add factory methods to make NSDictionary representations of the object and initialize new objects with a NSDictionary
There are pros and cons to both approaches. We've opted to go with the latter because it is closer to working with network services and APIs later on in the class.
-
Write a
dictionaryCopy
method that returns anNSDictionary
with keys and values matching the properties of the object.- note: Avoid using 'Magic Strings' in your code. Create private string keys for the class for each property that will be stored to and pulled from a NSDictionary (ex.
static NSString * const TimeStampKey = @"timestamp"
)
- note: Avoid using 'Magic Strings' in your code. Create private string keys for the class for each property that will be stored to and pulled from a NSDictionary (ex.
-
Write an initializer that takes a NSDictionary as a parameter and sets the timestamp, title, and text properties using the values from the dictionary.
- note: Check to see if any of the expected values from the NSDictionary are nil, return nil if any of the values are missing
- note: A safety check here is not required, but is the equivalent of a
guard let
in Swift
Our EntryController object is the source of truth for entries. We are now adding a layer of persistent storage, so we need to update our EntryController to load entries from NSUserDefaults on initialization, and save the entries to NSUserDefaults when they are updated.
-
Write a method called
- (void)saveToPersistentStorage
that will save the current entries array to NSUserDefaults- note: Manually loop through the entries
NSArray
to serialize anNSArray
of plist compatible dictionary copies - note: Avoid 'Magic Strings' when saving to NSUserDefaults
- note: Manually loop through the entries
-
Write a method called
- (void)loadFromPersistentStorage
that will load saved dictionary entries from NSUserDefaults and set self.entries to the results- note: Use the Entry
initWithDictionary(NSDictionary *)dictionary
while looping through the array to turn the dictionaries into Entry class objects
- note: Use the Entry
-
Call the
- (void)loadFromPersistentStorage
method when theEntryController
is initialized -
Call the
- (void)saveToPersistentStorage
any time that the list of entries is modified
- Implement the NSCoding protocol on the Entry class
- Create a Unit test that verifies NSCoding methodality by converting an instance to and from NSData
- Refactor persistence to work natively with Entry objects
Please refer to CONTRIBUTING.md.
© DevMountain LLC, 2015. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.