Today’s topics

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.

Drag 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:

the content in the text field is copied to the label,

the text field is emptied/cleared, and

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:

Left-side Navigator area – show or hide, you decide (Command+0)

Storyboard, select (click) the scene (view) that you want to work with

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)

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.)

Click and drag to the .h interface source code, until the insertion pointer appears, then release the mouse button (or touchpad)

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.

.

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: