DPS923 notes – Thu Jan 16

String-number conversions. Introduction to collections. Begin working with iOS apps, and essential user interaction.

.

Today’s topics

Notes and guidance will be added soon. Today’s topics:

  • String-number conversions
  • Introduction to collections
  • Begin working with iOS apps, and essential user interaction

.

String-number conversions

This is not a very exciting topic. However, it’s very important, because it helps a new iOS programmer make progress.

Below, you will learn about these two user interface controls:

Label – text displayed on a view; cannot be edited by the user

Text field – text displayed on a view; can be edited by the user

The important characteristic of both controls is that they work with “text”, specifically an NSString object. Therefore, when we need to display a number, or get numeric input, we need to do a string-number conversion.

.

Number to string

Use the NSString stringWithFormat: method.

// int to NSString
NSString *myInt = [NSString stringWithFormat:@”%d”, 123];

// double to NSString
NSString *myDouble = [NSString stringWithFormat:@”%1.2f”, 123.45];

.

String to number

Use the NSString intValue or doubleValue methods.

// NSString to int
int myInt = [@”123″ intValue];

// NSString to double
double myDouble = [@”123.45″ doubleValue];

In the reference documentation, notice these statements:

  • whitespace is ignored
  • returns 0 (or 0.0) if the text does not begin with a numeric digit

Also interesting is the fact that a string “123abc” will be converted to an int with value 123. In other words, trailing characters are ignored.

.

Begin working with iOS apps, and essential user interaction

In Xcode, create a new project, specifically:
iOS Application >  Single View Application, for iPhone only

Study the project’s structure, it has three new source code assets when compared to an OS X command line tool:

  1. AppDelegate – handles app lifecycle events (launch, interrupt because of incoming phone call, etc.), and app state transitions (e.g. active, background, etc.)
  2. Main.storyboard – container that holds the app’s scenes (i.e. the user interface)
  3. ViewController – manages one scene (view)

Learn more about the AppDelegate in this introduction. The “Overview” section of the UIApplication Class Reference document is good. Also, the “Overview” section of the UIApplicationDelegate Protocol Reference document is good.

Learn more about the storyboard in this introduction.

Learn more about a controller in this introduction. The “Overview” section of the UIViewController Class Reference document is good.

Run (Command+R), and the iOS Simulator will open your app.

While using the iOS Simulator app:

  • Shift+Command+H = performs a “home” button press
  • Command+Q = quit the simulator app
  • Command+3 = scale window to the smallest size
  • Command+2 = scale window a bit larger

Look at the iOS Simulator app’s menus to learn more about what it can do for you.

Click/select Main.storyboard, and the app’s scene will load in the editor. Look at the right-side Utility area, and notice the Attributes Inspector (fourth icon from left).

At the bottom of the Utility area, notice the Object library (third icon from left). That list shows the user interface objects that can be dragged to a scene.

We will work with these three objects in our first example, to ’round trip’ data from the user and back to the scene:

Scene with text field, button, labelDrag these objects to the user interface. Notice that sizing handles appear when an object is selected. Arrange them to appear similar to the diagram shown on the right. The idea is that when the app runs, the user will tap on the text field. A keyboard will appear, and the user can begin typing. When the user taps the button:

  1. the content in the text field is copied to the label,
  2. the text field is emptied/cleared, and
  3. the keyboard is dismissed.

What is the data type of a user interface object? Click to select one of the objects. Then, in the Utility area, look at the Identity Inspector (third icon from the left), which shows the class name for the user interface control. For example, the text field’s class name is UITextField.

Use the reference documentation to learn about a user interface control class. Using a web browser, use a search engine with these words:
apple uitextfield class reference

Usually, the first choice is the best choice in the list of search results (if it’s from developer.apple.com).

In Xcode, the screen layout when designing the app’s user interface shows these areas:

  1. Left-side Navigator area – show or hide, you decide (Command+0)
  2. Storyboard, select (click) the scene (view) that you want to work with
  3. Then press Option+Command+Return to open the Assistant Editor, which shows the scene’s controller; make sure that the controller’s .h interface source code file is displayed (Control+Command+up-arrow to toggle)
  4. Right-side Utility area – should show that, because we’ll need it (toggle with Option+Command+0)

How do you get programmatic access to objects in the user interface? Two important ways for new iOS programmers are to use outlets and actions. (A third way, which you will use soon, is known as a delegate.)

What is… an outlet, and an action?

  • Outlet – an object (in your class) that points to a user interface object
  • Action – the method that’s called when an event occurs (e.g. value changed, button tap, etc.)

Creating an outlet or action for a user interface object:

Click to select / highlight a user interface object. Then:

  1. Press and hold the Control key
  2. Click and drag to the .h interface source code, until the insertion pointer appears, then release the mouse button (or touchpad)
  3. Complete the configuration of the dialog that pops up

The “Name” field is for the outlet or action name that appears in your source code. The name should begin with a lowercase letter. Multi-word names begin with a lowercase letter, but use camel-casing for the remaining words (e.g. userInput, or doSomething).

The following shows the dialog for an outlet (text field), and for an action (button). Click to view them full-size in a new tab/window.

Outlet, create, for text field

.

Action, create, for button

Tip: When configuring an action, set the Type to the actual type of the user interface control.

.

(we’ll continue in our next class session)

.

Back to label, text field, and button…

Always ask yourself: What kind of interaction do I need with the user interface control?

  • None
  • Get info from it by asking for the info
  • Configure it with new/changed info
  • Be notified when something happens to it (i.e. an “event”)
  • …or some combination of the above

That will tell you whether you need an outlet or action – on the list above:

  • “None” needs neither an outlet or action,
  • Get or set info needs an outlet, and
  • Event-handling needs an action

Segmented Control

Slider

Switch

Stepper

Combine Slider with a Text Field

.

(more to come)

.

.

.

.

.

.

.

.

.

.

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: