아이폰 UITextField 키보드 핸들링 소스

iPhone UITextField Tutorial: Handling Keyboard Interactions

This post is written to provide you with the process to get the return button working on the keypad that pops up while filling a text field, the “Background Tap” functionality, and also what to do when the text field hides behind the keypad.

First we make a demo View-based Application for this, say “BackgroundTapForBlog” .
Add a text field to BackgroundTapForBlogViewController.xib, declare it in your BackgroundTapForBlogViewController.h file ,say tfUsername and link them in .xib.

Return Button on KeyPad:

Once you are done with this, add a function “textFieldDoneEditing” to the BackgroundTapForBlogViewController.m file, and do not forget to declare it in the BackgroundTapForBlogViewController.h file. This function gets rid of the keypad once you are done filling in the textfield.

BackgroundTapForBlogViewController.h file -

1 #import
2 @interface BackgroundTapForBlogViewController: UIViewController{
3 UITextField *tfUsername;
4 }
5 @property(nonatomic, retain) IBOutlet UITextField *tfUsername;
6 -(IBAction) textFieldDoneEditing : (id) sender;
7 -(IBAction) backgroundTap:(id) sender;
8 @end

BackgroundTapForBlogViewController.m file -

01 #import "BackgroundTapForBlogViewController.h"
02  
03 @implementation BackgroundTapForBlogViewController
04 @synthesize tfUsername;
05  
06 - (void)viewDidUnload {
07 self.tfUsername = nil;
08 }
09  
10 - (void)dealloc {
11 [tfUsername release];
12 [super dealloc];
13 }
14  
15 -(IBAction) textFieldDoneEditing : (id) sender{
16 [sender resignFirstResponder];
17 }
18  
19 -(IBAction) backgroundTap:(id) sender{
20 [self.tfUsername resignFirstResponder];
21 }
22 @end

You can download the source code from here.

http://www.mobisoftinfotech.com/blog/iphone/iphone-uitextfield-tutorial-handling-keyboard-interactions/http://www.mobisoftinfotech.com/blog/iphone/iphone-uitextfield-tutorial-handling-keyboard-interactions/

Posted by 오늘마감