0

My Header file

MyClass.h
@interface MyClass : UIViewController<UITextFieldDelegate>
{
}
@property (retain, nonatomic) IBOutlet UITextField *customValue;

MyClass.m

- (void)viewDidLoad
{
    customValue.delegate=self;
}
- (void)textFieldDidBeginEditing:(UITextField *)customValue
{
    NSLog(@"custom tips value %@",customValue.text);
}

My NSLog is printing the message, but the customValue.text is not being displayed and is coming as null.

EDIT 1 I need to get the values that is entered in the textfiled as and when the user is enter the value

4
  • hey what you want exactly?? here begin of textfield , textfield is null , so please what u want exactly Dec 4, 2012 at 10:13
  • 1
    Dude at start the text will be nil. Dec 4, 2012 at 10:14
  • You must have connect delegete from XIB Dec 4, 2012 at 10:14
  • Fix this question's title, because textfieldDidBeginEditing works in your question, as you describe.
    – Morkrom
    Aug 1, 2013 at 20:18

6 Answers 6

2

textFieldDidBeginEditing as it name indicates , it will work when you start editing on the textField.

For accomplishing your requirement you need to use the shouldChangeCharactersInRange delegate method.

- (BOOL)textField:(UITextField *)e shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *substring = textField.text;
    substring = [substring stringByAppendingString:string];
    NSLog(@"Text : %@",substring);
    return YES;
}

textFieldDidBeginEditing:

Tells the delegate that editing began for the specified text field.

- (void)textFieldDidBeginEditing:(UITextField *)textField Parameters

textField

The text field for which an editing session began.

Discussion

This method notifies the delegate that the specified text field just became the first responder. You can use this method to update your delegate’s state information. For example, you might use this method to show overlay views that should be visible while editing.

Implementation of this method by the delegate is optional. Availability

Available in iOS 2.0 and later.

Declared In UITextField.h

textField:shouldChangeCharactersInRange:replacementString:

Asks the delegate if the specified text should be changed.

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

Parameters

textField

The text field containing the text.

range

The range of characters to be replaced 

string

The replacement string. 

Return Value

YES if the specified text range should be replaced; otherwise, NO to keep the old text. Discussion

The text field calls this method whenever the user types a new character in the text field or deletes an existing character. Availability

Available in iOS 2.0 and later.

Declared In UITextField.h

For more check UITextFieldDelegate

1

This is because textFieldDidBeginEditing: is called on the user first touch of the textField.

To get the textField's text as the user changes it, connect the "Editing Changed" event to the File's Owner's IBAction.

1
  • Thank you for your answer. I had the same problem; the reason I forgot to connect my text field's Editing Changed event to the .h file of my view controller. Sep 26, 2013 at 20:32
1

Use This

- (BOOL)textField:(UITextField *)customTipsValue shouldChangeCharactersInRange:(NSRange)range 
 replacementString:(NSString *)string
{
NSLog(@"textfield value %@",customTipsValue.text);
return YES;
}// return NO to not change text
1
  • This snippet is working, but it is eating one character. I have entered 123, but Log is showing me 12
    – onkar
    Dec 4, 2012 at 10:29
0

Have you noticed that the method

- (void)textFieldDidBeginEditing:(UITextField *)customTipsValue

Is called when you start to edit the textField, not when you enter characters.

Check more info here:

UITextField Reference

0

just set clear textfield is no when its begin to edit mode..

- (void)viewDidLoad
{
       customValue.delegate=self;
       [customValue setClearsOnBeginEditing:NO];// Add this line
}
0

You will need this textField:shouldChangeCharactersInRange:replacementString:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.