0

iPhone and iPad Tutorial: Working with the textfield and keyboard

Posted by Dr. Burton on September 5, 2010 in iPad, iPhone, Mobile |

Today we are going to begin looking at using some of the controls available to us in xCode.  The first of these tools is the textbox and keyboard.

Using the textbox is very intuitive for most users, but it may require a little programming on your part to get everything to work the way the user (and Apple!) expects.

We will being a new project for this example.  I am calling mine Textbox.  This is a standard View-Based application.  You can of course do this as either an iPhone or iPad project.

First, a few things to consider:

Applications can have Active, Static, & Passive Controls
  • Active controls – trigger a method or piece of code
  • Static controls – can’t be directly interacted with by the user (labels)
  • Passive controls – hold values for the user until needed (sliders, textfield)
All controls are subclasses of the UIControl class – they can trigger methods or be used as static or passive controls.

To begin with, add the following lines to your TextboxViewController.h file:
@interface Control_FunViewController : UIViewController {

UITextField  *nameField;

UITextField  *numberField;

}

@property (nonatomic, retain) IBOutlet UITextField *nameField;

@property (nonatomic, retain) IBOutlet UITextField *numberField;

@end

And to your .m file add:

@synthesize nameField;

@synthesize numberField;

Don’t for get to release both of these:

– (void)dealloc {
[nameField release];
[numberField release];

[super dealloc];

IB

After saving both files, open up the IB and add two labels and two textfields.  The labels will only be used as identifiers, which is why they don’t have an IBOutlet associated with them.
Put the two labels on the left of the view, one below the other.  Then place two textfields on the view, each one across from a label.  Go ahead and put “Name” and “Number” in the two labels.

The textfield has a variety of options available.  In this case we are going to use the ‘placeholder’ to give the user instructions as to what should be entered into the textfield.  Type in “Enter Your Name” and “Enter Your Number”.

iPhone App Development-Textbox/Keyboard Controls (pt 1 of 2)
Runtime
7:24
View count
1,167

Now, connect the name textfield to the file owner and select “nameField”. Repeat for the number field selecting “numberField”.

Build and Run.  You should be able to enter name and number information into the fields.  But there is a problem, isn’t there?  The keyboard won’t go away!

iPhone App Development-Textbox/Keyboard Controls (pt 2 of 2)
Runtime
5:12
View count
1,483

First, let’s give the app the ability to dismiss the keyboard.  In the header (.h) file add:

- (IBAction)doneEditing:(id)sender;

In the main (.m) file, add:

-(IBAction) doneEditing:(id)sender {

[sender resignFirstResponder];

}

Now in the IB, select the nameField, open the connections (Command-2) and connect Did End On Exit with the File Owner.  Select doneEditing.  Repeat for numberField.

Now when you Build & Run, the keyboard will dismiss from the name field when you click on Return. But there is no Return button for the number field.  We need another method to dismiss the keyboard.

Add to the header file:

- (IBAction) backgroundClick:(id)sender;

To the main file:

- (IBAction)backgroundClick:(id)sender {

[nameField resignFirstResponder];

[numberField resignFirstResponder];

}

Open the IB.  Place a button in the top left corner of your view, then resize it to fill the view.  Then select ‘Touch Up Inside’ and connect it to the File Owner and select backgroundClick.

Now under the Tools menu, select send to back.

Go ahead and Build & Run.  Now when you click outside the textfields, the keyboard is dismissed!

Be Sociable, Share!

Tags: , ,

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree

Copyright © 2010-2014 Burtons Media Group All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.2.4.1, from BuyNowShop.com.

Privacy Policy