Home > 2010 Fall DPS913 > Get started with data conversion and keyboard handling

Get started with data conversion and keyboard handling

September 16, 2010 Leave a comment Go to comments

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

.

Data type conversions

A task that you must get comfortable with really fast is data type conversion. The user interface elements that enable user input offer values from a variety of data types.

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

From text 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). You also learned that the keyboard stays on screen until you make it disappear.

We discussed (the first responder)…

If you have only one text entry UI element on your view, you can use a very simple strategy to dismiss the keyboard: In the method that handles the processing of the user input (e.g. a button), send a resignFirstResponder message to the UI element.

If you have more than one, …

In portrait mode, the slide-up keyboard is 216 pixels in height. If the location of the text entry UI element (e.g. a UITextField) will be underneath the keyboard, you must shift the view “up”, so that the UITextField is visible. (more)

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

.


Advertisements
Categories: 2010 Fall DPS913
  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: