I’m having a problem with a Segmented Control in a Toolbar on my Keyboard in my current iOS App. I’m trying to basically have a keyboard with a Toolbar that has a Segment control on the left hand side with Previous/Next options and then a Done button. I got it all setup and everything is firing correctly except for the one thing I want to do is make sure that the correct segment is pre-selected when the appropriate text box is selected. The problem I’ve been running into is that no matter what text field I click on, the wrong segment is selected and I have to push it twice to get it in the right order.
Here is the code of the toolbar being created (this is in ViewDidLoad)
keyBar = [[UIToolbar alloc] init];
[keyBar setBarStyle:UIBarStyleBlack];
[keyBar sizeToFit];
segControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Previous", @"Next", nil]];
[segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
segControl.selectedSegmentIndex = 0;
[segControl addTarget:self action:@selector(segSelected:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:segControl];
UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignKeyboard)];
NSArray *itemArray = [NSArray arrayWithObjects:segButton, flexButton, doneButton, nil];
[segButton release];
[flexButton release];
[doneButton release];
[keyBar setItems:itemArray];
And here is how I add the bar to both text fields in my table (using a custom cell as well)
#pragma mark - User Name Row
if (section == 0 && row == 0) {
static NSString *TextEntryCell = @"TextCell";
TextCell *customCell = (TextCell *)[tableView dequeueReusableCellWithIdentifier:TextEntryCell];
if (customCell == nil) {
NSString *nibName = nil;
if (iPad) {
nibName = @"TextCell_iPad";
}
else {
nibName = @"TextCell";
}
NSArray *outlets = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
for (id currentObject in outlets) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
customCell = (TextCell *)currentObject;
break;
}
}
}
customCell.selectionStyle = UITableViewCellSelectionStyleNone;
customCell.typeLbl.text = @"User Name:";
customCell.textField.placeholder = @"Enter User Name";
customCell.textField.returnKeyType = UIReturnKeyNext;
customCell.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
customCell.textField.tag = 1;
//segControl.selectedSegmentIndex = 0;
[customCell.textField setInputAccessoryView:keyBar];
return customCell;
}
#pragma mark - Password Row
if (section == 0 && row == 1) {
static NSString *TextEntryCell = @"TextCell";
TextCell *customCell = (TextCell *)[tableView dequeueReusableCellWithIdentifier:TextEntryCell];
if (customCell == nil) {
NSString *nibName = nil;
if (iPad) {
nibName = @"TextCell_iPad";
}
else {
nibName = @"TextCell";
}
NSArray *outlets = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
for (id currentObject in outlets) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
customCell = (TextCell *)currentObject;
break;
}
}
}
customCell.selectionStyle = UITableViewCellSelectionStyleNone;
customCell.typeLbl.text = @"Passphrase:";
customCell.textField.placeholder = @"Enter Passphrase";
customCell.textField.returnKeyType = UIReturnKeyGo;
customCell.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
customCell.textField.tag = 2;
segControl.selectedSegmentIndex = 1;
[customCell.textField setInputAccessoryView:keyBar];
return customCell;
}
So as you can see I basically want it setup so that when the first Text box is selected then the Previous segment in the segment control is already selected and then the the same for the second text box and the next segment. But no matter what I’ve tried so far when I click on either text box the second segment is always the default selected item so in the first Textfield it’s a bit clunky since I have to click both indexes then it starts to work right. After I do that then it works perfectly, but it’s just that initial display that is giving me an issue.
You cannot add the bar to both views – its one object and you cannot share it. You should create two such bars, add a unique one to both views. Keep a reference to both segmented controls, and update both to reflect the selected text box. I believe that architecture will solve this. If not, after you do it, update the question or add a comment to this answer.