Reputation: 6824
I am trying to create a next button that works with the textField's tag. I have this inside a void type method:
for (UITextField *textField in self.view.subviews)
{
if ([textField isKindOfClass:[UITextField class]])
{
if([textField isFirstResponder])
{
int i = _textFieldTag; //starts as 0
[[textField viewWithTag:i] resignFirstResponder];
NSString *a = [(UITextField *)[textField viewWithTag:i] text];
NSLog(@"TEXT 01 - %@", a);
i = i + 1;
NSLog(@"TAG 02 - %i", i);
[[textField viewWithTag:i] becomeFirstResponder];
NSString *b = [(UITextField *)[textField viewWithTag:i] text];
NSLog(@"TEXT 02 - %@", b);
}
}
}
The problem is that even though i
increments by 1, NSString *b
returns nil and does not make that textField the next responder like I am expecting.
They are there, the tag exists, but somehow the new value of i is not accepted.
Any ideas?
Upvotes: 0
Views: 117
Reputation: 104092
You logic is not correct, and as @Greg said, you need to use [self.view viewWithTag:i]
to get the correct view. Also, give self.view a tag that's different from any of the text fields' tags (you got the error because self.view has the default tag of 0). I think you want your logic to look like this:
-(IBAction)nextField:(id)sender {
int i;
for (UITextField *textField in self.view.subviews) {
if ([textField isKindOfClass:[UITextField class]] && [textField isFirstResponder]) {
[textField resignFirstResponder];
NSString *a = textField.text;
NSLog(@"TEXT 01 - %@", a);
//i = textField.tag + 1; // use this line if you don't want to go back to the first text field
i = (textField.tag == 4)? 0 : textField.tag + 1; //use this line if you want to cycle back to the first text field when you are on the last one. Replace the 4 with whatever is your highest tag. This also assumes that the tag of the first text field is 0.
}
}
NSLog(@"TAG 02 - %i", i);
[[self.view viewWithTag:i] becomeFirstResponder];
NSString *b = [(UITextField *)[self.view viewWithTag:i] text];
NSLog(@"TEXT 02 - %@", b);
}
Upvotes: 0
Reputation: 25459
Try replace all of your call from:
[textField viewWithTag:i]
to:
[self.view viewWithTag:i]
You should ask view for viewWithTag not UITextField.
Upvotes: 2