iphone textfield focus to send field - iphone

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.

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.

4 digit password type view: number and cursor control

First posting to Stackoverflow, but have been searching for answers for sometime now.
Learning Objective-C & XCode and have been creating simple projects.
Currently, I wanted to attempt a screen where user will enter in a 4 digit password code, where each individual digit in seperate text fields.
1) How to make cursor be in first text field, causing keyboard to come up automatically when app is started
2) How to make the text field only accept one digit. Can you do this in interface builder, in the attributes for the text field? Or do you have to do that programmatically?
3) How to make cursor jump to next text field after previous one is filled. Will this happen automatically when question #2 is done?
Would appreciate any help... thanks!
Ok here you go
1. You must create 4 UITextFields in the IB say textField1, textField2, textField3, textField4. and call [textField1 becomesFirstResponder]; this will make the cursor in your textfield1.
2.To make textfield accept only one digit, you will have to do this
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
textField.text = string;
if ([string length] > 0) {
if ([textField isEqual:textField1]) {
[textField2 becomeFirstResponder];
}else if([textField isEqual:textField2]) {
[textField3 becomeFirstResponder];
}else if([textField isEqual:textField3]) {
[textField4 becomeFirstResponder];
}else if([textField isEqual:textField4]) {
[textField resignFirstResponder];
}
}
return FALSE;
}
you will have to do this programmatically.
3.same as ans2.
Create a hidden uitextfield that you are actually typing into. Then create four 'seen' uitextfields and add a bullet character into each one as the user types into the hidden field.
This is the easiest way to do what you want.

Keypressed event in UITextField

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.

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.

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;
}