iPhone – UITextField Delegate Methods…

So its a long time….Okkk.Let me tell you about the delegates and those methods.Here we go,

What is Delegate:

A delegate is an object which communicates with other objects when an event occurs.If you keep your fingers over a candle light you get a message to your brain which says to take back,its a kinda delegate.

UITextField Delegate Methods:

Every component has some delegate methods which will be called when an  event occurs.TextField delegate methods are,

*textFieldShouldBeginEditing

*textFieldShouldEndEditing

*textFieldDidBeginEditing

*textFieldDidEndEditing

*textField shouldChangeCharactersInRange

*textFieldShouldClear

*textFieldShouldReturn

Now Lets have a Deep look over them,

textFieldShouldBeginEditing:

This method asks  some textfield to become the first responder(Current TextField).So you can change the delegate information.

textFieldDidEndEditing:

This method asks  some textfield to resign the first responder(Current TextField).So you can change the delegate information.

textFieldDidBeginEditing:

This methods tells that some textfield has just became the first responder(Current TextField).So you can change the delegate information.

textFieldDidEndEditing:

This methods tells that some textfield has just resigned the first responder(Current TextField).So you can change the delegate information.

textField shouldChangeCharactersInRange:

This method is called whenever user types a new character or delete’s a character.

textFieldShouldClear:

This method is used to clear the textfield which is an build in method.

textFieldShouldReturn:

This method is called when the user enter’s the return button.So we can add some additional behaviour here.

 Example:

Step 1: As i have explained already i have created an Xcode project.

Step 2: The xib file looks like this.I have two textfields for UserName and Password with a button.

screen

Step 3: I have created outlet and set delegate to the textfields.

Setdelegate

Step 4: Implement UITextFieldDelegate in the .h file.

setDelegateinInterfaceStep 5: I have included the QuartzCore framework and imported the file in my .m file.Using this you can make some overlay colouring,modify border width and more.

import

Step 6: Implement your logic inside the delegate methods.

Now you are set to Go….

Code:

.h File:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>

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

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

– (IBAction)submitClicked:(id)sender;

@end

 .m File:

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@end

@implementation ViewController

@synthesize userNameTextfield,passwordTextfield;

– (void)viewDidLoad

{

[super viewDidLoad];

}

– (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

}

– (void)dealloc {

[userNameTextfield release];

[passwordTextfield release];

[super dealloc];

}

– (IBAction)submitClicked:(id)sender {

if(userNameTextfield.text.length == 0 || passwordTextfield.text.length==0)

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Please Enter UserName and Password” message:nil delegate:self cancelButtonTitle:@”Ok” otherButtonTitles:nil];

[alert show];

}

else

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”You are learning about delegates….” message:nil delegate:self cancelButtonTitle:@”Ok” otherButtonTitles:nil];

[alert show];

}

}

#pragma  – mark Textfield Delegate Methods

– (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

textField.layer.borderWidth=2;

textField.layer.borderColor=[[UIColor redColor] CGColor];

textField.layer.cornerRadius=7;

return YES;

}

– (void)textFieldDidBeginEditing:(UITextField *)textField  {

}

– (BOOL)textFieldShouldEndEditing:(UITextField *)textField

{

textField.layer.borderColor = [[UIColor  clearColor] CGColor];

return YES;

}

– (void)textFieldDidEndEditing:(UITextField *)textField {

}

– (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {

return YES;

}

– (BOOL)textFieldShouldClear:(UITextField *)textField  {

return NO;

}

– (BOOL)textFieldShouldReturn:(UITextField *)textField

{

if(textField == userNameTextfield)

{

[passwordTextfield becomeFirstResponder];

}

else if (textField == passwordTextfield)

{

[passwordTextfield resignFirstResponder];

}

return YES;

}

@end

Advertisements

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