DPS923 notes – Mon Jan 27

Collections. User interface objects that use a collection as a data binding source. Hands-on.

.

Collections

Yes, we introduced this topic on January 16. However, that shouldn’t have happened, because you could not immediately apply the new knowledge.

So today, we’ll cover this again. Then apply the concepts in code. Here’s what you can expect:

  • Common collection classes – dictionary, array, set
  • Internal structure of a collection object
  • Boxing (wrapping) a C scalar value (e.g. int, double, float) into an object so that it can be in a collection
  • Unboxing an object to extract a C scalar value
  • Declare and initialize
  • Getting access to a collection’s members
  • Mutability scenarios
  • Matching a collection to a user interface object – picker view, table view

.

Concept of a “delegate”

Very important.

A “delegate” is an object that performs tasks for another object.

In some ways, like a personal assistant.

The delegate must adopt and follow a behaviour protocol, defined by the other object.

In code, the interface will list the adopted protocol, and the interface will implement the protocol’s methods.

Why can’t the other object simply do the tasks itself? Perhaps the object’s source code is not available, so you are unable to define its behaviour. Or perhaps there’s a need to change from one delegate to another at runtime.

Have we seen this before? Yes. The UITextField requires a delegate to perform some tasks.

Why are we talking about this now? Because we will introduce two more user interface objects that require delegates.

.

Introducing the picker view and the table view user interface objects

“A picker view lets the user choose between certain options [using a spinning wheel metaphor]. Picker views are well-suited for choosing [among] a moderate number of discrete options.” (Picker Views, from the UIKit User Interface Catalog)

“A table view presents data in a scrollable [single-column] list of rows. It is a means for displaying and editing hierarchical lists of information. Table views provide a simple yet versatile interface for managing and interacting with collections of data.” (Table Views, from the UIKit User Interface Catalog)

Both of these user interface objects require a delegate object. For our purposes, the delegate is the controller that is managing the view on which you place the picker view or table view.

In addition, both require a data source object. As above, the data source is the controller that is managing the view.

In the controller (which is the delegate and data source), we implement methods that will perform tasks during the user interface object’s life cycle.

Who or what calls a method (or in Objective-C, “sends a message”)? The Cocoa runtime, in response to a user action or a system event.

Typical “data source” methods 

The controller will handle these data source oriented messages for a picker view:

  • Number of components (i.e. spinning wheel segments) in the picker view
  • Number of rows in a specific component

Typical “delegate” methods

The controller will handle these delegate oriented messages for a picker view

  • Get the displayable content for a specific row in a specific component
  • The user selected a specific row in a specific component, so perform a task

.

Working with a picker view

There are a number of steps involved in getting a single-component picker view working:

  1. Create a collection that will be the picker view’s data source
  2. Add the picker view to the scene
  3. Configure its delegate and dataSource properties
  4. Update the controller’s .h interface
  5. Implement methods in the controller’s .m interface

.

Create a collection that will be the picker view’s data source

In the Model object, create a collection that will be the picker view’s data source.

In class today, we’ll create two collections. One statically-defined, the other programmatically generated.

.

Add the picker view to the scene

On a scene, click-drag-drop a picker view, and position it appropriately.

Notice that it will show placeholder text as a design-time aid.

.

Configure its delegate and dataSource properties

Select to highlight the picker view.

Ctrl+click-drag to the “View Controller” icon in the scene’s dock. On the popup, select the delegate property.

Do this again, and select the dataSource property.

.

Update the controller’s .h interface

At the end of the @interface… statement, adopt the required protocols with this syntax:

@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>

.

Implement methods in the controller’s .m interface

Implement and write code for these four methods:

  1. numberOfComponentsInPickerView:
  2. pickerView:numberOfRowsInComponent:
  3. pickerView:titleForRow:forComponent:
  4. pickerView:didSelectRow:inComponent:

As you are learning, when you write code for the pickerView:didSelectRow:inComponent: method, you can output the result to either the debug console or to a label user interface control.

Study/skim the UIPickerView class reference documentation, and:

.

Working with a table view

We’ll do this during the next class session.

.

.

.

.

.

.

.

.

.

.

.

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: