Keypressed event in UITextField - iphone

I have 10 textfields, in which I could enter only one character in each textfield. After a character is entered in each textfield, the focus should move to the next one. Similarly when i delete character from a textfield by pressing the backspace or delete, i need to get the focus to the previous textfield. If I could get the keypressed event, I could do that. Right now I am not able to find any keypressed event examples.

Implement UITextFieldDelegate.
Implement the delegate methods in the protocol. You can achieve the things you wanted.
You can set the focus by using the method becomeFirstResponder to the required textfield.
Have a look at the delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
The text field calls this method whenever the user types a new character in the text field or deletes an existing character.
So that could solve your problem.

Based on Aadhira's answer, but taking into account Kirk Woll's comment, you can generate what the latest text will be by using stringByReplacingCharactersInRange:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *value = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(#"value: %#", value);
return YES;
}

Just to give you directions:
Assign tag to each text field.
Implement UITextFieldDelegate. There are all the methods you need to detect any event that takes place inside the text field. In each method you can check the tag and move focus properly.
Hint: you can use [mainView viewWithTag:XX] to quickly pick the text field you need.
Each time the text is changed you can check the text property of the text field and it will give you the answer which button was pressed.

you have to implement the UITextFieldDelegate protocol into your code and this method will tell you when you start begin editing in text field
– textFieldShouldBeginEditing:
and you can set the if condition in this method according to your requirement...

You have to use the textfieldDelegate methods.
In your textFieldShouldReturn method you have to set your responders like
if (textfield == textField1)
{
[textField2 becomeFirstResponder];
}
else if (textField == textField2)
{
[textField3 becomeFirstResponder];
}
else
{
[textField3 resignFirstResponder];
}
return YES; // as method return type is BOOL.

Related

How to set UILabel's value according to UITextField as it is typed in iPhone?

I want to set UILabel according to the UITextField as it is typed. I mean if user want to type SAMPLE and he starts typing S then the lable should be set as S, then he types A label should also be A and so on. How to achieve this?
Please share suggestions.
Thanks in advance
Simplest way to do is to make a method and connect it with UiTextfield with event UIControlEventEditingChanged which will give you the trace on every character entered in the textfield.
[self.selectedTextField addTarget:self action:#selector(enterInLabel ) forControlEvents:UIControlEventEditingChanged];
-(void)enterInLabel
{
selectedLabel.text=selectedTextField.text;
}
The delegate also works for this as #brain said. The shouldChangeCharactersInRange: method can be a little confusing but the the following I think is pretty straight forward.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// myTextField delegate has been set
if ([textField isEqual:myTextField]){
NSMutableString *txt = [NSMutableString stringWithString:textField.text];
[txt replaceCharactersInRange:range withString:string]; //this is essentially how the textfield is updated after YES is returned
previewLabel.text = txt;
}
return YES;
}
The changing of the text field is actually done after the return YES. That is the whole point of this delegate. Just for an example, if you wanted to limit a textfield to 3 characters you could do the following to stop the text field from "replacing" the text.
if (range.location > 3)
return NO;
Set your textField delegate then call its method in your viewController.m file.. like this -
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
mylabel.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
return YES;
}
This will change the text after each increment of character.
OR You can add action on your TextField
See this link - UiTextField events
Your view controller will need to implement the UITextFieldDelegate which will allow it to receive changes to the text field. In the appropriate methods of the delegate you need to set the text of the UILabel.
I have never used UITextFieldDelegate so can't provide more detail on how to use it. I would mock up a quick example and just NSLog or debug the delegate calls to see that calls you get.

how to clear the text field by using the delegate functions

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length >=8)
{
return NO; // return NO to not change text
}
else {
return YES;
}
}
when i am adding this method to my program, text will not be clear.
how can i clear my text field. by using the below method
- (BOOL)textFieldShouldClear:(UITextField *)textField
The behavior you are seeing does not depend on textFieldShouldClear:, whose default implementation already returns YES (source):
The text field calls this method in response to the user pressing the built-in clear button. (This button is not shown by default but can be enabled by changing the value in the clearButtonMode property of the text field.) This method is also called when editing begins and the clearsOnBeginEditing property of the text field is set to YES.
The problem lays with textField:shouldChangeCharactersInRange: denying any change whenever the textField contains more that 8 characters:
if (textField.text.length >=8) {
return NO; // return NO to not change text
I don't know why you set this or if you could find another way to get the same, but if you want to leave it like this, then a possible workaround is checking the replacementString and if it is empty, allow the text change by returning YES.
If you want a more sophisticated solution, you could think of setting an ivar flag when textFieldShouldClear: is called, so that when you find the flag set in textField:shouldChangeCharactersInRange:, you return YES.
- (BOOL)textFieldShouldClear:(UITextField *)textField {
self.shouldClearTextCalled = YES;
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (self.shouldClearTextCalled)
return YES;
self.shouldClearTextCalled = NO;
if (textField.text.length >=8) {
return NO; // return NO to not change text
} else {
return YES;
}
}

Control should switch automatically after the user is done entering the text in UITextField

In my app i have two textfields and i want the control from one text field to switch to second control as soon as the user is done entering the values in the first textfield automatically.
Does anybody has any idea on this?
Thanks,
I assume "done entering" means the user hits return, ... button on keyboard.
UITextField has delegate with UITextFieldDelegate protocol. And here's method ...
- (BOOL)textFieldShouldReturn:(UITextField *)textField
... you can implement it in this way ...
...
self.myFirstTextField.delegate = self;
self.mySecondTextField.delegate = self;
...
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ( textField == self.myFirstTextField ) {
// User hits return key on keyboard when editing first textfield
[mySecondTextField becomeFirstResponder];
} else if ( textField == self.mySecondTextField ) {
// User hits return key on keyboard when editing second textfield
// This simply removes focus from the second textfield and hides keyboard
[mySecondTextField resignFirstResponder];
}
return YES;
}
If "done entering" is not return, ... button on keyboard, you can implement this delegate's method ...
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
... to check UITextField's text when user did type.

iphone textfield focus to send field

hello all i have my four textfield for entering password If my password is "1 2 3 4", is there a way to automatically jump to the second box as soon as I enter "1" and so on and so forth?
use a UITextViewDelegate to detect the input and then call becomeFirstResponder on the next text field.
you need to make your class conform to UITesxtFieldDelegate and then use delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(texField == textfield1)
{
if([textfield1.text isEqualToString: #"1"])
{
[textfield1 resignFirstResponder];
[textfield2 becomeFirstResponder];
}
}
else if(texField == textfield2)
...... // write similar code
}
Text field calls this method whenever user enters some text or delete text from textfield. Also you need to set in your viewdidload method textfield1.delegate=self; for each textfield you need to call this method. For further details read doc for UITextFieldDelegate.

delete last character UITextField

I have an UITextField and I would like that for every tap on a character, the first character is deleted. So that I just have one character in my textField every time. Moreover I would like it to display every tap in the console log.
How can I do this?
You need to implement shouldChangeCharactersInRange method in your text field delegate:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:
(NSRange)range replacementString:(NSString *)string{
textField.text = #"";
return YES;
}
You may need to check for range and string values to cover all possible cases (like copy/paste actions). This code just sets the text field's value to the last typed character.
UITextField inherits from UIControl, so you can use the target-action mechanism that is part of the UIControl class:
[textField addTarget:self action:#selector(updateTextField) forControlEvents:UIControlEventValueChanged];
In the action method, you can replace the UITextField's text with only the last character and log that character in the console. Note that since changing the UITextField's text will again result in the "updateTextField" message being sent a second time to the target, you will need some kind of mechanism for determining whether to update or not:
- (void)updateTextField {
if(updateTextField == YES) {
updateTextField = NO;
NSString *lastChar = [textField.text substringFromIndex:[textField.text length]];
[textField setText:lastChar];
NSLog(#"%#", lastChar);
} else {
updateTextField = YES;
}
}
Or something like that anyway...
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.text.length > 8) {
return NO;
}
return YES;
}