In my view controller I have 5 Text Fields, 4 become first responder with keyboard and one with the UIPicker, they are all tagged +1 (0,1,2...)
It just so happens that the textfield that becomes first responder with the Picker is number 3 so its stuck in the middle. In my other fields I have my code set so that the "Next" return button takes me to my next field, but I haven't been able to pull the same off with the picker, code that makes this happen:
-(BOOL) textFieldShouldReturn:(UITextField *) theTextField {
if (theTextField == self.textFieldE) {
[theTextField resignFirstResponder]; //This of course being the last text field
} else {
[(UITextField *)[theTextField.superview viewWithTag:theTextField.tag+1]
becomeFirstResponder];
}
return YES;
}
The UIPickerView has an accessory view attached to it with a done button, that does in fact resignFirstResponder
but I need to be able to add a prev and next button to make firstResponder either -1.tag or +1.tag since the picker is stuck in position number 3.
Here is my picker code:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == self.textOutlet) {
self.textOutlet.inputView = _thePicker;
self.thePicker.hidden = NO;
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackTranslucent;
[toolbar sizeToFit];
//to make the done button aligned to the right
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:self
action:@selector(doneClicked:)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
self.textOutlet.inputAccessoryView = toolbar;
}
}
and of course the action of the "Done" button:
-(void)doneClicked:(id) sender
{
[_textOutlet resignFirstResponder]; //hides the pickerView
}
Thanks for the help in advance!
Try this code. It should work as you want.
#define SEGMENTED_CONTROL_TAG 1567 // random
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == self.textOutlet) {
self.textOutlet.inputView = _thePicker;
self.thePicker.hidden = NO;
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackTranslucent;
[toolbar sizeToFit];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Previous", @"Next", nil]];
segmentedControl.tag = theTextField.tag + SEGMENTED_CONTROL_TAG;
[segmentedControl addTarget:self action:@selector(textFieldContinue:) forControlEvents:UIControlEventValueChanged];
//to make the done button aligned to the right
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:self
action:@selector(doneClicked:)];
[toolbar setItems:@[segmentedControl,flexibleSpaceLeft, doneButton]];
self.textOutlet.inputAccessoryView = toolbar;
}
}
-(IBAction) textFieldContinue:(UISegmentedControl*)control{
UITextField *textField;
if(control.selectedSegmentIndex == 0) {
textField = (UITextField*)[self.view viewWithTag:control.tag-1 - SEGMENTED_CONTROL_TAG];
} else if (control.selectedSegmentIndex == 1) {
textField = (UITextField*)[self.view viewWithTag:control.tag+1 - SEGMENTED_CONTROL_TAG];
}
if(textField) {
[textField becomeFirstResponder];
}
}