DPS923 MAP523 Lab 4

Two-level navigation-based app, using Core Data for persistence.

.

DPS923 MAP523 Lab 4 – due Thu Feb 12

Due date: Thursday, February 12, 2015, at 9:45am ET

Grade value: 4% of your final course grade

.

Objective(s)

Navigation-based app style, with two levels (list and detail)

Use Core Data for persistence

.

Introduction to the problem that you will solve

We need an app that will display information about Canadian provinces.

The app’s first view will be a table view, showing a list of province names. Tapping one of the rows will display a ‘details’ page, with some information about the province.

Data will be created when the app is launched for the first time.

Read through this assignment fully before starting work on it. That way you will know what’s coming.

These images are an example of what you will create:

run-provinces-list run-provinces-detail

.

Specifications

Get started by copying the “ClassesV1” project template. Follow the ‘rename project’ procedure, and the project’s new name will be “Canada”.

Next, you will perform these tasks:

  1. Design the Province entity
  2. Generate custom subclass for the entity
  3. Write code in the store initializer to create startup data for the app
  4. Create a fetched results controller for the province collection
  5. Create a controller for the list view
  6. Create a controller for the detail view
  7. Update and design the storyboard scenes/views
  8. Update the code in the app delegate and new controllers
  9. Clean up the project

.

Design the Province entity

In the Core Data model editor, delete the existing “Example” entity.

Apple has a guide, last updated in September 2013, that may be somewhat useful:

Core Data Model Editor Help

.

Add a new entity, named “Province”. Then add attributes:

object-model-province-entity

.

Generate custom subclass for the entity

Select the entity (in the model editor).

Then, on the Editor menu, choose “Create NSManagedObject Subclass…”.

It will create a “Province.swift” source code file and Province class.

Important:

One more task… follow these instructions to edit the name of the class in the Data Model Inspector.

Its class name must be fully qualified – in other words, it must be ‘projectName.className’.

.

Write code in the store initializer to create startup data for the app

In the store initializer, write code to create three (3) Province objects. You can get descriptive data from Wikipedia.

Follow the guidance suggested by the existing code in the template. Adding a custom subclass for the Province entity simplifies the syntax. Here’s an example of the correct code:

store-init-add-province

.

The store initializer will be called from the init method of the model object.

At this point, you can probably build your app to ensure there are no compiler errors.

.

Create a fetched results controller for the province collection

Study the Model class, and the existing NSFetchedResultsController property. Notice that it has three replaceable items:

  1. the property’s name
  2. the name of the entity
  3. the sort descriptors

We suggest that you copy-paste this code block to create a new property, and then edit/change the replaceable items.

.

Create a controller for the list view

Add a new iOS > Source > Cocoa Touch Class.

Subclass of UITableViewController.

Name should be something like “ProvinceList”.

Other edits will be discussed later.

.

Create a controller for the detail view

Add a new iOS > Source > Cocoa Touch Class.

Subclass of UIViewController.

Name should be something like “ProvinceDetail”.

It needs a property to hold the Province object that will be passed to it during the ‘list’ controller’s prepareForSegue(sender: ) method.

Other edits will be discussed later.

.

Update and design the storyboard scenes/views

The ClassesV1 template does include a storyboard with the navigation controller, and two scenes (list and details). The scenes point to ExampleList and ExampleDetail.

Change this. On each scene, click the controller icon (in the scene’s dock). Then, on the Identity Inspector, set the class to the appropriate new controller class (ProvinceList and ProvinceDetail).

Also, you should change the segue identifier (name). It should be ‘toProvinceDetail’ or something like it.

.

Edit/design the ‘list’ scene

Change the title, to something like “Provinces”. Any other changes? Probably not.

.

Edit/design the ‘details’ scene

Add simple labels to the ‘details’ scene. Here’s an example:

scene-detail

.

Add constraints, if necessary.

Make connections to outlets in the controller. Other edits will be discussed below.

.

Update the app delegate code

If you study the app delegate’s application(didFinishLaunchingWithOptions: ) method, you will notice that the table view controller was set to an “ExampleList” type.

Change that to a “ProvinceList” type.

.

Update the controller code

Study the code in the “ProvinceList” controller. It was created by the Xcode ‘new item’ template. The code is OK, but is not suitable for our purposes.

The code in the “ExampleList” controller is better. It is better-organized, and has all the features we need.

Therefore, a good strategy would be to:

  1. copy the code from ExampleList
  2. paste it to ProvinceList, replacing all the code
  3. edit to customize the code to the Province entity

.

Therefore, do that. There are a number of places where you must edit the code, including:

  • the name of the class
  • in viewDidLoad(), the statement that assigns a fetched results controller to the frc variable
  • in configureCell(atIndexPath: ), which fetches a specific Province object, and renders content from it
  • in prepareForSegue(sender: ), a number of edits are needed…
  • the segue identifier comparison string
  • fetching the specific Province object
  • configuring the title of the destination view controller

.

run-provinces-detail-unformattedEdits to ProvinceDetail

This is a simpler task.

Assign the values to the user interface labels. Use string interpolation when appropriate.

.

Formatting the number and date values

It is likely that your ‘area’ and ‘date’ values will not be formatted, as shown in the example to the right.

Date and number formatting is a big topic for any programming platform, so here are the basics. To format the ‘area’ number to include commas – which is normal for North American users – use a method named descriptionWithLocale(abbreviation: ):

province-detail-format-number

 

.

To format the ‘date’ to be more readable, you must create and configure a date formatter object:

 

province-detail-format-date

.

Clean up the project

After the app runs correctly, you can clean up the project.

Remove the ExampleList and ExampleDetail controllers.

Make sure your source code is commented, where appropriate.

Are you interested in reformatting your source code? The easiest way is to select all (Command+A) in a source code file, and press Ctrl+I (which is Editor > Structure > Re-indent).

.

Submitting your work

Follow these instructions to send me your work:

  1. Make sure your project works correctly
  2. Locate your Canada project folder in Finder
  3. Right-click the folder, and choose Compress “Canada”, which creates a zip file (make sure the zip file is fairly small)
  4. Login to My.Seneca, and in this course’s Assignments area, look for the upload link, and submit your work there

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: