Get started with data conversion and keyboard handling

There are typically two things that beginners need to learn for most iOS apps – data conversion, and keyboard handling.

This document was most recently updated in September 2012.

.

Data type conversions

A task that you must get comfortable with really quickly is data type conversion. The user interface elements that enable user input offer values from a variety of data types, so you need to be adept at this task.

For example, a UITextField – a simple text box – offers a text property, which is an NSString. The UILabel also offers a similar text property.

A UISlider offers a value property, which is a float.

If you want to build an app that reports the current value of a slider to the label, or enables the user to set the value of the slider by typing into a text box, then you will have to perform data type conversions.

Therefore, you must learn the general pattern for data type conversions. The pattern requires that you know what data types are available, and their characteristics.

Remember – Objective-C is a superset of C – all the scalar types in C are available for your use, plus the object types that Objective-C adds. Therefore, you perform arithmetic on the C types. Do not try and look for ways to do arithmetic on Cocoa objects.

From text (typically a Cocoa string) to a C scalar number:

  • Send the text a message – intValue, doubleValue, floatValue

From C scalar number to text:

  • Use an NSString class method, like [NSString stringWithFormat:@”%d”, myInteger];

From C scalar number to an NSNumber:

  • Use a class initializer, like [NSNumber numberWithInt:myInteger];

The “Conversions” example app shows many typical conversions. Check the comments in the code for more details.

.

Keyboard management

As you learned in class, the device keyboard appears when you are in a text entry field (like a UITextField) It slides up from the bottom, and covers almost half the screen. You also learned that the keyboard stays on screen until you make it disappear. How do you dismiss the keyboard? The answer depends upon your scenario.

.

Three typical scenarios

There are three typical user interface scenarios:

  1. There is only one UITextField, and it is on the top half of the screen. In addition, there is a UIButton that submits the user input. (This is the simplest scenario.)
  2. There are multiple UITextField controls, and one (or more) of them is located on the bottom half of the screen. You therefore need to move the view up so that the UITextField is not hidden by the keyboard.
  3. There is one or more UITextView controls.

The first two scenarios are discussed below. The third scenario, involving UITextView controls, will be covered a bit later in the course.

.

One UITextField

In this scenario, where a UIButton tap calls a method, it is really easy to dismiss the keyboard.

Simply send the UITextField the resignFirstResponder message. For example, if you have a text field named tfName, use this statement:

[self.tfName resignFirstResponder];

.

Multiple UITextFields, some of which may be under the keyboard

In this scenario, there are two distinct tasks to perform:

  1. Detect whether the user intends to edit a specific UITextField
  2. Detect whether the keyboard will cover UITextField

.

For task 1, open the view nib in the Interface Builder editor. Show/display the Connections inspector. For each UITextField…

  1. Click to select it
  2. Connect the delegate to the File’s Owner (i.e. the view controller)

.

Then, edit the .h interface of the view controller, to make it conform to the UITextFieldDelegate protocol, as shown in the following example:

@interface ViewController : UIViewController <UITextFieldDelegate>

Then, you can implement two delegate methods:

– textFieldDidBeginEditing:
– textFieldDidEndEditing:

When the user taps a UITextField, the Cocoa runtime will call your textFieldDidBeginEditing: method, and send it a reference to the UITextField that was tapped. This enables your code to determine the UITextField’s current position, and if it is under the keyboard, you can move the view controller’s view up, so that the UITextField is visible.

When the user performs an action which results in the UITextField losing its first responder status, the Cocoa runtime will call your textFieldDidEndEditing: method, and send it a reference to the UITextField that lost first responder status. (The action could be one of a number of user actions – for example, a different UITextField was tapped, or a UIButton was tapped, or some other user interface event.) This enables your code to perform clean-up tasks.

.

Example apps

The “ViewShifting” example app shows how this is done. Check the comments in the code for more details. (The “Conversions” example app also has an implementation of this concept.)

.


.

.

.

  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: