iphone TextField


iphone TextField

In this tutorial will create table view and in table view cell will add different Text Field and in that you can enter the text.

In this tutorial will create table view and in table view cell will add different Text Field and in that you can enter the text.

iphone TextField

In this tutorial we will create table view and in table view cell that will add different Text Field and in that you can enter the text. This application is based on Navigation based application and in that will add another View Controller and coding will be done in Root View Controller and the View Controller which we added to our application.

Final View will look like this:

My project name is TextField and is based on Navigation Based Application, this is because in Navigation Based Application will have table View and there is no need to add the table view and initialize it. To add the data into the Table view will take an array will declare property in .h file and synthesize it in .m file.

Add this to RootViewCntroller.h file:

NSMutableArray *menuList;

@property (nonatomic, retain) NSMutableArray *menuList;

After synthesizing will load the data into the Table View and to do this will write it in viewDidLoad method, here am adding only one data to the table view and the name for the data is Text Field title and as we know that in Navigation based application you will have navigation bar and so for that am setting the title as Various Text Field and as you select the cell in table view will take you to another view and that view also contain table and in that Table cell will have different Text Field, this is done in View Controller file which we added to our project. In viewWillAppear will set the index path for the selected row, set number of section in Table View, number rows in each section and then customize the cell of the Table View.

Add this to RootViewController.m file:

#import "TextFieldController.h"

static NSString *kTitleKey = @"title"static NSString *kViewControllerKey = @"viewController"; static NSString *kCellIdentifier = @"MyIdentifier";

@synthesize menuList;

- (void)viewDidLoad {   [super viewDidLoad];

 self.menuList = [NSMutableArray array];  TextFieldController *textFieldViewController = [[TextFieldController alloc]

 initWithNibName:@"TextFieldController" bundle:nil];   

[self.menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:

 NSLocalizedString(@"TextFieldTitle", @""), kTitleKey, textFieldViewController, kViewControllerKey, nil]];

 [textFieldViewController release];   self.navigationItem.title = @" Various Text Field ";

- (void)viewWillAppear:(BOOL)animated {

 NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow];  

  [self.tableView deselectRowAtIndexPath:tableSelection animated:NO]; }

- (void)viewDidUnload {   

  [super viewDidUnload];    self.menuList = nil;  }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;  }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  return [self.menuList count]; }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];   if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];

 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   }

 cell.textLabel.text = [[self.menuList objectAtIndex:indexPath.row] objectForKey:kTitleKey];   return cell; }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 UIViewController *targetViewController = [[self.menuList objectAtIndex: indexPath.row] objectForKey:kViewControllerKey];

[[self navigationController] pushViewController:targetViewController animated:YES];   }

After adding the data into the Table View, will write the code for the text field which we have to add into the another Table View. Now that we are adding the Text Field into the Table View will declare variable for each type and also an array to after declaring will declare the property for each variable and will synthesize it in .m file so that the getter and setter will be created for the variable automatically.

Add this to ViewController.h file:

UITextField  *textFieldNormal; UITextField   *textFieldRounded; UITextField  *textFieldSecure;  

 UITextField  *textFieldLeftView;   

NSArray   *dataSourceArray;

@property (nonatomic, retain, readonly) UITextField   *textFieldNormal;

@property (nonatomic, retain, readonly) UITextField   *textFieldRounded;

@property (nonatomic, retain, readonly) UITextField   *textFieldSecure;

@property (nonatomic, retain, readonly) UITextField   *textFieldLeftView;

@property (nonatomic, retain) NSArray *dataSourceArray;

First define the some constant variable and after that synthesize it and then will add the data into the Table View in viewDidLoad method, Text Fields are added into the Table View with the help of an array which we created in .h file and using that will add different Text Field into the Table View, will set normal, round, secure and left view Text Field into the Table View. We are not editing any field, it will be in Edit when user touches the edit button. Release the controls and set them nil in case they were ever created and Note that we cannot use "self.variablename = nil" since that can read only property, this will be done in viewDidUnload method.

Then will set the number of sections in table view by counting the array and then will set the title for the header in section and also the number of rows in section. and to determine specific row height for each cell, override this and this is done by heightForRowAtIndexPath.
After that will determine which UITableViewCell to be used on a given row. Finally will create the Text Field and this done by coding, will set everything for the text field, will start with Frame, boarder Style, text color, font, place holder, background color keyboard type and the return key type to hide the keyboard and the clear button is used to clear some data in text field, similarly is done in remaining three text field (In each Text Field there is small changes like in last one am adding on image into the text field that to on the left side of the text field ).

Add this to ViewController.m file:

#define kTextFieldWidth       260.0

#define kLeftMargin                20.0

#define kTextFieldHeight       30.0

static NSString *kSectionTitleKey = @"sectionTitleKey";  static NSString *kSourceKey = @"sourceKey"; static NSString *kViewKey = @"viewKey";  

 const NSInteger kViewTag = 1;

@synthesize textFieldNormal, textFieldRounded, textFieldSecure, textFieldLeftView, dataSourceArray;

- (void)viewDidLoad{   [super viewDidLoad];

    self.dataSourceArray = [NSArray arrayWithObjects: [NSDictionary dictionaryWithObjectsAndKeys: @"UITextField", kSectionTitleKey,

 @"TextFieldController.m: textFieldNormal", kSourceKey, self.textFieldNormal, kViewKey, nil],

 [NSDictionary dictionaryWithObjectsAndKeys: @"UITextField Rounded", kSectionTitleKey, @"TextFieldController.m: textFieldRounded", kSourceKey,

 self.textFieldRounded, kViewKey, nil],

[NSDictionary dictionaryWithObjectsAndKeys:  @"UITextField Secure", kSectionTitleKey,@"TextFieldController.m: textFieldSecure", kSourceKey,

self.textFieldSecure, kViewKey, nil],

[NSDictionary dictionaryWithObjectsAndKeys: @"UITextField (with LeftView)", kSectionTitleKey, @"TextFieldController.m: textFieldLeftView", kSourceKey,

self.textFieldLeftView, kViewKey, nil], nil];    self.title = NSLocalizedString(@"TextFieldTitle", @"");

 self.editing = NO;   }

- (void)viewDidUnload {

    [super viewDidUnload];    [textFieldNormal release];   

  textFieldNormal = nil;    [textFieldRounded release];  

 textFieldRounded = nil;    [textFieldSecure release];

    textFieldSecure = nil;    [textFieldLeftView release]; 

   textFieldLeftView = nil;    self.dataSourceArray = nil;     }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return [dataSourceArray count];   }

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

 return [[self.dataSourceArray objectAtIndex: section] valueForKey:kSectionTitleKey];       }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 2;    }

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return ([indexPath row] == 0) ? 50.0 : 22.0;    }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = nil 

   NSUInteger row = [indexPath row];     

  if (row == 0){

 static NSString *kCellTextField_ID = @"CellTextField_ID";

 cell = [tableView dequeueReusableCellWithIdentifier:kCellTextField_ID];     if (cell == nil){

 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellTextField_ID] autorelease];

 cell.selectionStyle = UITableViewCellSelectionStyleNone;     }   


 UIView *viewToCheck = nil;  viewToCheck = [cell.contentView viewWithTag:kViewTag];      if (!viewToCheck)

[viewToCheck removeFromSuperview];      }

 UITextField *textField = [[self.dataSourceArray objectAtIndex: indexPath.section] valueForKey:kViewKey];

[cell.contentView addSubview:textField];    }   

 else {

static NSString *kSourceCell_ID = @"SourceCell_ID";

cell = [tableView dequeueReusableCellWithIdentifier:kSourceCell_ID];     if (cell == nil){

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSourceCell_ID] autorelease];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.textLabel.textAlignment = UITextAlignmentCenter;  cell.textLabel.textColor = [UIColor grayColor];

 cell.textLabel.highlightedTextColor = [UIColor blackColor]; 

cell.textLabel.font = [UIFont systemFontOfSize:12];    }

cell.textLabel.text = [[self.dataSourceArray objectAtIndex: indexPath.section] valueForKey:kSourceKey];    }

 return cell;    }

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

 [textField resignFirstResponder];    return YES; }

- (UITextField *)textFieldNormal{  if (textFieldNormal == nil){

 CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight); 

  textFieldNormal = [[UITextField alloc] initWithFrame:frame];

 textFieldNormal.borderStyle = UITextBorderStyleBezel textFieldNormal.textColor = [UIColor blackColor]; 

textFieldNormal.font = [UIFont systemFontOfSize:12.0];  textFieldNormal.placeholder = @"<enter text>";  

textFieldNormal.backgroundColor = [UIColor whiteColor];   textFieldNormal.autocorrectionType = UITextAutocorrectionTypeNo; 

 textFieldNormal.keyboardType = UIKeyboardTypeDefaulttextFieldNormal.returnKeyType = UIReturnKeyDone;

 textFieldNormal.clearButtonMode = UITextFieldViewModeWhileEditing; textFieldNormal.tag = kViewTag;      

 textFieldNormal.delegate = self;       }     return textFieldNormal;  }

- (UITextField *)textFieldRounded  {    if (textFieldRounded == nil) {

CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight); 

textFieldRounded = [[UITextField alloc] initWithFrame:frame];   textFieldRounded.borderStyle = UITextBorderStyleRoundedRect

 textFieldRounded.textColor = [UIColor blackColor];   textFieldRounded.font = [UIFont systemFontOfSize:17.0];    

 textFieldRounded.placeholder = @"<enter text>";   textFieldRounded.backgroundColor = [UIColor whiteColor];   

textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo;    textFieldRounded.keyboardType = UIKeyboardTypeDefault;

    textFieldRounded.returnKeyType = UIReturnKeyDone;     textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing 

textFieldRounded.tag = kViewTag;         

 textFieldRounded.delegate = self;     }    return textFieldRounded;   }

- (UITextField *)textFieldSecure{   if (textFieldSecure == nil){

 CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);   textFieldSecure = [[UITextField alloc] initWithFrame:frame];

 textFieldSecure.borderStyle = UITextBorderStyleBezel; textFieldSecure.textColor = [UIColor blackColor];

textFieldSecure.font = [UIFont systemFontOfSize:27.0]; textFieldSecure.placeholder = @"<enter password>";

textFieldSecure.backgroundColor = [UIColor whiteColor];   textFieldSecure.keyboardType = UIKeyboardTypeDefault;

 textFieldSecure.returnKeyType = UIReturnKeyDone;    textFieldSecure.secureTextEntry = YES;

textFieldSecure.clearButtonMode = UITextFieldViewModeWhileEditing;   textFieldSecure.tag = kViewTag;   textFieldSecure.delegate = self;    }

  return textFieldSecure;     }

- (UITextField *)textFieldLeftView  {

    if (textFieldLeftView == nil)   {     CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);

 textFieldLeftView = [[UITextField alloc] initWithFrame:frame];    textFieldLeftView.borderStyle = UITextBorderStyleBezel;

 textFieldLeftView.textColor = [UIColor blackColor];     textFieldLeftView.font = [UIFont systemFontOfSize:09.0];

textFieldLeftView.placeholder = @"<enter text>";      textFieldLeftView.backgroundColor = [UIColor whiteColor];

 textFieldLeftView.keyboardType = UIKeyboardTypeDefault;     textFieldLeftView.returnKeyType = UIReturnKeyDone;   

 textFieldLeftView.clearButtonMode = UITextFieldViewModeWhileEditing;   textFieldLeftView.tag = kViewTag;        

textFieldLeftView.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbutton.jpg"]];

textFieldLeftView.leftViewMode = UITextFieldViewModeAlways

  textFieldLeftView.delegate = self;   }

    return textFieldLeftView;    }

Finally Press Build And Go Button

Download the code