DPS923 notes – Thu Mar 13

Programming assignment discussion. Topics that contribute to the programming assignment.

.

Programming assignment discussion

We will discuss and design the entities for the app.

In addition, we will discuss the functionality that you need to program for ‘deliverable 1’.

The results will be in the first specifications document.

.

Model object inheritance

Equipment entities share some properties. So, you could just repeat these properties, but there’s another way.

Model objects support inheritance. Slick. How?

First, create an entity (maybe named “Equipment”), and add some properties.

Next, in the Data Model Inspector for the entity, check/mark the “Abstract Entity” check box.

Then, create an entity that will inherit from Equipment (maybe named “Helmet”). Add some properties.

Finally, in the Data Model Inspector for the entity, select “Equipment” from the “Parent Entity” drop-down list.

.

Model versions and data migration

Here’s a scenario: You have an existing app, and it has an existing object model. Now you need to add more properties or entities, and you want to keep your data. How?

Model versions are the answer. For many kinds of changes, it’s automatic, and it saves (and migrates) your data. Sweet. Apple calls this Lightweight Migration. How do you configure it?

Before starting, make sure that your project is using ClassesV2 or later as its base/template. Then:

.

1. In the Project Navigator, select your object model item, ObjectModel.xcdatamodeld. Then, show the File Inspector panel.

.

2. On the Editor menu, choose Add Model Version, and complete the dialog.

In the Project Navigator, you will see the following. Notice that the “ObjectModel.xcdatamodel” item has a green check-mark icon:

model-versioning-added-version

On the File Inspector panel, you will see a new configuration item:

model-versioning-selector-before

.

3. Change the Current Model Version (in the drop-down list) to the new model version that you just added.

model-versioning-selector-after

In the Project Navigator, notice that the new “ObjectModelV2.xcdatamodel” item now has the green check-mark icon:

model-versioning-added-version-selected

.

Now you can add the new properties and entities. The next time your app loads, a “lightweight migration” will be performed.

More information is available in the Core Data Model Versioning and Data Migration Programming Guide, in the first three sections:

  • Understanding Versions
  • Model File Format and Versions
  • Lightweight Migration

.

Standalone fetch request

A fetched results controller object is used with a table view.

If you are not using a table view, how do you query the on-device data store? By using a fetch request object.

Previously, you have learned that a fetched results controller includes a fetch request property.

You can use a fetch request object to perform a get-all, get-some-filtered, or get-one query. For all of these situations, the results come back as an array that has zero or more objects.

A get-all query will not need a predicate.

A get-some-filtered or get-one query will need a predicate. Predicate string format examples are fully documented here (although some will not work with a Core Data store).

For any query that will return a collection, you can choose to configure sort behaviour.

.

Follow this guidance to code your first standalone fetch request. In this scenario, we are fetching the Team object that’s used in the Winter 2014 programming assignment. There will be only one team object, so the fetched results will be an array with exactly one object in it.

For best results, in your Model class, create a declared property for the team object. Then, write a custom getter in the Model class implementation. In the custom getter method code block:

Create a fetch request object:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@”Team”];

Next, ask yourself whether you need a predicate or sorting. For this situation, the answer will be “no”, so you will not need to write code to create a predicate object or a sort descriptors object.

You will need to declare an error object, in case something goes wrong. Then, ask the managed object context to execute the fetch request. The results come back as an array.

fetch-request-execute

.

Scrollable view

Used for content that is taller than one iPhone screen.

A “Scroll View” can be easily added to a scene that’s managed by a standard ‘view controller’ (UIViewController). However, working with a scrollable view on the storyboard requires some configuration.

Here’s a step-by-step guide to adding and configuring a scroll view.

.

1. On the storyboard, show the document outline

It’s the icon in the lower-left area of the storyboard.

show-document-outline

.

2. Select and size the View Controller item in the document outline

First, select the View Controller item in the document outline.

view-controller-select-in-document-outline

Next, show its Size Inspector.

Change the Simulated Size to Freeform. Then, enter your desired size. An iPhone screen is 320 wide, and normally 480 tall. So, you can enter a new height value, for example 600 or 800.

view-controller-size-inspector

At this point in time, the View Controller scene on the storyboard will resize itself, to the new height.

.

3. Add a Scroll View to the scene

From the user interface Object Library, locate the Scroll View item. Select it, then click-drag-and-drop it on to the view. Be careful where you place it. Use both guides (horizontal and vertical).

.

4. Add a View to the scene

From the user interface Object Library, locate the View item.

view-in-object-library

Select it, then click-drag-and-drop it on to the scroll view. Be careful where you place it. Use both guides (horizontal and vertical).

.

5. For the view (added in step 4), pin its height

Select the view that you added in step 4.

view-pin-height

On the Editor menu, choose Pin, then Height.

.

6. For the view (added in step 4), resolve auto layout issues

Again, select the view that you added in step 4.

On the Editor menu, choose Resolve Auto Layout Issues, then Add Missing Constraints In View Controller.

A number of ‘constraints’ will be added to the document outline hierarchy.

.

7. Add your desired user interface objects and controls

Now, add labels, text fields, buttons, and so on to the view that was added in step 4. That view is the ‘container’ view for our user interface objects and controls.

After you add some items, look at the document outline, and the scene. It will look something like this. Click to open it full-size in a new tab/window.

scroll-document-outline-and-scene

.

8. Build and run

At this point in time, you should be able to build and run, and scroll the view up-and-down.

See the “Scroll” code example on the GitHub code example repository, in the Week_09 folder.

.

Detail disclosure button

Here’s what you know: If you have a table view that offers drill down navigation to another detailed view, you know that you can tap anywhere on the row to perform the navigation.

What if you have multi-level navigation? For example, think about a music app, that would offer drill down navigation, from Artists, to Albums, to Songs. The app’s first scene would show a list of Artists. Tap an Artist, and it would navigate to a list of Albums from that Artist. Tap an Album, and it would navigate to a list of Songs on that Album from that Artist.

OK, good so far. However, when looking at a list of Artists, is it possible to drill down to get more information about that Artist?

Yes. How? By using a detail disclosure button.

When the user taps on the detail disclosure button – shown inside the green rectangle below – it can drill down (navigate) to a view that shows more information about that selected row.

However, if the user taps anywhere else on the row – show as the red rectangle below – a normal drill down navigation, to a list of Albums from that Artist.

detail-disclosure-running

.

How do you implement this?

.

1. Add a new controller

Continuing with our scenario, a UIViewController subclass named ArtistView would be created.

.

2. Add a new scene to the storyboard

Add a new View Controller object to the storyboard. In its Identity Inspector, specify the ArtistView custom controller name.

Add the user interface objects you want (e.g. labels etc.). Create outlets as needed in ArtistView.h.

.

3. Change the ‘accessory’ on the ArtistList scene

On the storyboard, select the “Prototype Cells” object on the ArtistList scene.

On its Attributes Inspector, change the “Accessory” setting to “Detail Disclosure”, as shown below:

detail-disclosure-config

.

4. Add a segue, from ArtistList, to ArtistView

On the storyboard, click/select the ArtistList scene, and locate the yellow ‘view controller’ icon on the scene’s dock.

Control+click-and-drag from that yellow ‘view controller’ icon, to the new ArtistView scene. On the popup, choose “push”. On the segue’s Attributes Inspector, add a suitable identifier (e.g. “toArtistView”).

.

5. Add code to ArtistList to handle the segue

How do you handle the detail disclosure button tap? With this new/added method:

tableView:accessoryButtonTappedForRowWithIndexPath:

Here’s a typical implementation. It programmatically triggers the segue. Also, ensure that you pass indexPath, because the performSegueWithIdentifier:sender: method will need that information.

detail-disclosure-handle

Then, in the performSegueWithIdentifier:sender: method, add another segue handler code block. A typical implementation would look like this:

detail-disclosure-segue

.

A code example titled CD 2 1-M + can be found in the Week_09 folder of the GitHub code example repository. Study its ArtistList, ArtistView, AlbumList, and AlbumView controllers, and the storyboard.

Note – this code example is not perfect. It was created for iOS 5, and does not use our current and modern best practices. Therefore, it can be used to learn how to implement the detail disclosure functionality, but its other parts need refreshing.

.

Toolbar

Located at the bottom of the scene, a toolbar enables additional functionality for a scene. Can be used on both a standard view, and a table view.

We will discuss the toolbar next week.

.

Search table view

Enables a list to be searched.

We will discuss search next week.

.

Other topics to be determined

Here’s a list of remaining topics (others may be added)

  • Working with images (and maybe other media)
  • 2D graphics
  • iPad: SplitViewController, Popover
  • Maybe: Localization
  • Maybe: API to Contacts, Calendar, Maps, Music, etc.
  • Maybe: reminder about photo/camera picker, email/share sheet, etc.

.

.

.

.

.

.

.

.

.

.

  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: