How to step through multiple UITextFields - iphone

I have four UITextFields that I would like to use in a UIAlertView, Currently this is what it is looking like
What I would like to be able to do is restrict each box to 4 characters once each field once that 4 character limit is reached then I would like the next UITextField to become the first responder.
I would also like to be able to do this in reverse so if characters are being deleted once there are no characters available in the second field go to the first and start deleting etc.
Currently my code is pretty sloppy as I am just trying to get something like this working.. so this is what it looks like so far.
//prompt user to enter registration here
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Please Register Device" message:#" " delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 53, 30)];
UITextField *myTextField1 = [[UITextField alloc] initWithFrame:CGRectMake(77, 45, 53, 30)];
UITextField *myTextField2 = [[UITextField alloc] initWithFrame:CGRectMake(142, 45, 53, 30)];
UITextField *myTextField3 = [[UITextField alloc] initWithFrame:CGRectMake(207, 45, 53, 30)];
// need to do something with first reasponder once 4 digits has been entered per box etc.
[myTextField becomeFirstResponder];
myTextField.placeholder =#"AAAA";
myTextField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField.textAlignment = NSTextAlignmentCenter;
myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myTextField1.placeholder =#"AAAA";
myTextField1.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField1.textAlignment = NSTextAlignmentCenter;
myTextField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myTextField2.placeholder =#"AAAA";
myTextField2.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField2.textAlignment = NSTextAlignmentCenter;
myTextField2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myTextField3.placeholder =#"AAAA";
myTextField3.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField3.textAlignment = NSTextAlignmentCenter;
myTextField3.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myTextField1 setBackgroundColor:[UIColor whiteColor]];
[myTextField2 setBackgroundColor:[UIColor whiteColor]];
[myTextField3 setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert addSubview:myTextField1];
[alert addSubview:myTextField2];
[alert addSubview:myTextField3];
[alert show];
Any help would be greatly appreciated.
// UPDATE:
this is how I am trying to use the delegate method
//.h
<UITextFieldDelegate>
//.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//..
[self.myTextField setDelegate:self];
[self.myTextField1 setDelegate:self];
[self.myTextField2 setDelegate:self];
[self.myTextField3 setDelegate:self];
//..
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(#"yay");
return NO;
}

Update
I tried the code below, and does work, but for a reason if you change the FirstResponder, the text won't get written. So I modified the function. You should do this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.text.length >= MAX_LENGTH)
return NO;
if((textField.text.length + string.length) >= MAX_LENGTH)
{
int currentTag = textField.tag;
if(currentTag == 4)
return YES;
UITextField *newTextField = (UITextField *) [textField.superview viewWithTag:(currentTag+1)];
[newTextField becomeFirstResponder];
textField.text = [textField.text stringByAppendingString:string];
}
return YES; //you probably want to review this...
}
Any way, this doesn't work when you wan't to delete content, but that's easy solved if you change the code to check the range of the changes.
I made a sample project, you can get it here: TextFieldTest
This is how I would do it, using the textfield delegate, you can check the new inserted characters, if you reach the maximum, you move to the next textfield:
static int MAX_LENGTH = 4;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if((textField.text.length + string.length) >= MAX_LENGTH)
{
//Get next TextField... A simple way to do this:
UITextField *newTextField = [textField.superview viewWithTag:(textField.tag+1)];
[newTextField becomeFirstResponder];
//remember to set the tags in order
}
return YES; //you probably want to review this...
}
(Sorry if dosen't compile I just wrote it here directly)
Good Luck!

You can set delegate for these text fields and implement your business logic (set one of the text fields as the new first responder) in the delegate methods.

Related

Resign keyboard of textfield present in alertview

I have three text fields on an alertview, I set keyboard as decimalType ,
- (IBAction)heightMethod:(id)sender
{
self.utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; utextfield.placeholder = #" Centimeters";
self.utextfield.delegate=self;
self.utextfield.tag=3;
[ self.utextfield setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview: self.utextfield];
// Adds a password Field
self.ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(40, 80.0, 80, 25.0)]; ptextfield.placeholder = #" Feet";
self.ptextfield.delegate=self;
self.ptextfield.tag=4;
[self.ptextfield setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:self.ptextfield];
self.ptextfieldInches = [[UITextField alloc] initWithFrame:CGRectMake(140, 80.0, 80, 25.0)]; ptextfieldInches.placeholder = #" Inches";
self.ptextfieldInches.delegate=self;
self.ptextfieldInches.tag=5;
[ptextfieldInches setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:ptextfieldInches];
[self.utextfield setKeyboardType:UIKeyboardTypeDecimalPad];
[self.ptextfieldInches setKeyboardType:UIKeyboardTypeDecimalPad];
[self.ptextfield setKeyboardType:UIKeyboardTypeDecimalPad];
[self.alertHeight show];
}
As I tap any textfield, keyboard resign only two times, but on third time its not resigning . I added resignfirst responder method inside the delgate method of alertview , look here
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Create iVar of UITextField and assign in side UITextFieldDelegate method textFieldShouldBeginEditing. Hopefully it should work.
Like Below:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField = iVar;
}
Try
- (void) alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self.view endEditing: YES];
}
When you Press OK button on alert view the keypad dismisses automatically.I have tested it out so remove resignfirstresponder from the above mentioned method and try

Detecting backspace in UITextField the right way

Attention! This question is not a duplicate of this or this question! This is a new question to make things more clear.
So I have followed all the instructions of the posts above and I have this UITextField that becomes empty and moves to a new position every time the user hits return button. Moreover, I trace the input of the textView and I create a label of that text in the position the TextView was, like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField.text.length > 5) {
CGRect labelFrame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 0, 0);
UILabel *label = [[UILabel alloc] initWithFrame: labelFrame];
label.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
[label setText:textField.text];
[label setTextColor: [BSFunctions getColorFromHex:#"3f3f3f"]];
label.backgroundColor =[UIColor lightGrayColor];
[label sizeToFit];
[labelsArray addObject:label];
[self.view addSubview: label];
CGRect newTextFieldFrame = CGRectMake(labelFrame.origin.x + label.frame.size.width + 5, labelFrame.origin.y, 320, 30);
NSLog(#"Rect is %#", NSStringFromCGRect(newTextFieldFrame));
textField.frame = newTextFieldFrame;
textField.text = #"\u200B";
}
return YES;
}
I set the text in UITextField text to be #"\u200B" and I then want to detect the backspace button on it like this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string isEqualToString:#""]) {
NSLog(#"backspace button pressed");
if (labelsArray.count > 0) {
UILabel *labelToDelete = [labelsArray lastObject];
CGRect labelPosition = labelToDelete.frame;
CGRect oldPosition = textField.frame;
textField.frame = CGRectMake(labelPosition.origin.x, labelPosition.origin.y, oldPosition.size.width, oldPosition.size.height);
[labelToDelete removeFromSuperview];
[labelsArray removeLastObject];
textField.text = #"\u200B";
}
}
return YES;
}
But the problem is, it works only once and then, even with the special character added at the beginning of the textField it doesn't work. What is possibly wrong?
After setting
textField.text = #"\u200B";
in textField:shouldChangeCharactersInRange:replacementString:
You will have to add
return NO;
otherwise it will continue to the return YES and replace it with a #"" and then when pressing the backspace once again nothing will be changed and the delegate method will not be called.
It is possible that I misunderstood your goal here, but I hope this helps.

wait_fences: failed to receive reply: 10004003

I have an alertview that is being created with a textfield inside of it. when i close the alertview via a submit or cancel button, i am getting the wait_fences error in the console. it doesnt crash, or i havent been able to make it crash but id really like to figure out what is going on.
alert = [[UIAlertView alloc]
initWithTitle:#"Lookup"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Submit", nil];
label = [[UILabel alloc] initWithFrame:CGRectMake(12, 40, 260, 25)];
label.font = [UIFont systemFontOfSize:16];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(0, -1);
label.textAlignment = UITextAlignmentCenter;
label.text = #"Enter 10-Digit ISBN Number";
[alert addSubview:label];
field = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
field.font = [UIFont systemFontOfSize:18];
field.backgroundColor = [UIColor whiteColor];
field.keyboardAppearance = UIKeyboardAppearanceAlert;
field.keyboardType = UIKeyboardTypeNumberPad;
field.borderStyle = UITextBorderStyleBezel;
field.delegate = self;
[field becomeFirstResponder];
[alert addSubview:field];
[alert show];
I looked around online to try and figure out what the problem was and some people had mentioned resigningFirst responder. I added that to - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex but it didnt do anything.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[field resignFirstResponder]
if (buttonIndex == 1) {
NSLog(#"Submit");
} else {
NSLog(#"Cancel");
}
}
I then added an if statement to try and find out if field was the first responder and i got nothing.
if([field isFirstResponder]) {
NSLog(#"field isFirstResponder");
}
does anyone have any suggestions of what i could have done wrong?
so i think ive got it figured out.
it seems that when i was calling [field resignFirstResponder] from alertView:didDismissWithButtonIndex, firstResponder wouldnt really resign and the keyboard would be forced out a second or two after the alertView had been removed. but when i call [field resignFirstResponder] from alertView:clickedButtonAtIndex it would resign firstResponder like it is supposed to.
I also ran into a problem were the alertview would move down before being removed, to compensate for the keyboard not being there, but to fix this i created a function thats sole purpose is to call [field resignFirstResponder] and called that from alertView:clickedButtonAtIndex and delayed it being run by .2 seconds.
Did you try removing textfield like:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[[alertView.subviews objectAtIndex:0] removeFromSuperview];
}

ResignFirstResponder doesn't dismiss the keyboard (iPhone)

I've searched through this site that I couldn't find a solution to the problem I'm facing now. Hope someone can help.
I've created a UIAlertView to prompt user to enter their name in an iPhone app.
UIAlertView *enterNameAlert = [[UIAlertView alloc] initWithTitle:#"Enter your name"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", nil)
otherButtonTitles:NSLocalizedString(#"OK", nil),nil];
UITextField *enterNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
enterNameField.keyboardAppearance = UIKeyboardAppearanceAlert;
enterNameField.borderStyle = UITextBorderStyleRoundedRect;
enterNameField.autocorrectionType = UITextAutocorrectionTypeNo;
enterNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
enterNameField.returnKeyType = UIReturnKeyDone;
enterNameField.delegate = self;
[enterNameField becomeFirstResponder];
[enterNameAlert addSubview:enterNameField];
[enterNameAlert show];
[enterNameAlert release];
[enterNameField release];
I've setup this viewController to comply with UITextFieldDelegate in the header file, and implemented textFieldShouldReturn: trying to dismiss the keyboard when user hit Done.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
NSLog(#"text field was first responder");
} else {
[textField becomeFirstResponder];
[textField resignFirstResponder];
NSLog(#"text field was not first responder");
}
NSLog(#"textfieldshouldreturn");
return YES;
}
In the debugger, I confirm that this method is called successfully when I tap on the Done button, with the textField being the first responder at that time. However, the keyboard doesn't go away, but the little clearButton goes away. It is clear that the textField is no longer the first responder because when I click the Done button again, nothing get called. I just want to dismiss the keyboard with the Done button. Can anyone please offer a solution? Million thanks.
First you need to define your enterNameAlert in interface header. then use the below code.
- (void)viewDidLoad {
[super viewDidLoad];
enterNameAlert = [[UIAlertView alloc] initWithTitle:#"Enter your name"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", nil)
otherButtonTitles:NSLocalizedString(#"OK", nil),nil];
UITextField *enterNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
enterNameField.keyboardAppearance = UIKeyboardAppearanceAlert;
enterNameField.borderStyle = UITextBorderStyleRoundedRect;
enterNameField.autocorrectionType = UITextAutocorrectionTypeNo;
enterNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
enterNameField.returnKeyType = UIReturnKeyDone;
enterNameField.delegate = self;
[enterNameAlert addSubview:enterNameField];
[enterNameField becomeFirstResponder];
[enterNameAlert show];
[enterNameField release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
[enterNameAlert dismissWithClickedButtonIndex:1 animated:YES];
[enterNameAlert release];
NSLog(#"text field was first responder");
} else {
[textField becomeFirstResponder];
[textField resignFirstResponder];
NSLog(#"text field was not first responder");
}
NSLog(#"textfieldshouldreturn");
return YES;
}
What happens if you move the releasing of the text field to the delegate method, after you call the resignFirstResponder method?
Try dismissing keyboard using resignFirstResponder on "DidEndOnExit" event of your TextField.
Then try to dismiss keyboard by pressing Done button on keyboard when you have finished entering the data into your TextField.
Hope this helps.
Have another object become and then immediately resign the first responder.
simply do
[mytextField addTarget:self
action:#selector(methodToFire:)
forControlEvents:UIControlEventEditingDidEndOnExit];
if anyone is having trouble with this in storyboard i had to go into my uitextfields properties and ctrl+drag from its delegate to its root view controller all in storyboard and that fixed everything..

Keyboard not dissmissing

In my view controller i added a text field as below.
txt1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 70, 20)];
txt1.delegate = self;
txt1.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:txt1];
[txt1 release];
I set UITextFieldDelegate in .h file. I write
-(BOOL)textFieldShouldReturn:(UITextField *)textField
to dissmiss my keyboard.But it not firing delegate method.
Any help!!!
Thanks in advance.
Have you tried sending the resignFirstResponder message?
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}