在我当前的 iOS 应用程序中,我的键盘上的工具栏中的分段控件出现问题。我试图基本上有一个带有工具栏的键盘,该工具栏在左侧有一个分段控件,带有上一个/下一个选项,然后是一个完成按钮。我完成了所有设置并且一切正常,除了我想做的一件事是确保在选择适当的文本框时预先选择了正确的段。我遇到的问题是,无论我单击哪个文本字段,都选择了错误的段,我必须按两次才能将其按正确的顺序排列。
这是正在创建的工具栏的代码(在 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];
这是我如何将栏添加到表格中的两个文本字段(也使用自定义单元格)
#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;
}
因此,如您所见,我基本上希望设置它,以便在选择第一个文本框时,则已经选择了段控件中的上一个段,然后将第二个文本框和下一个段的段相同。但是,无论我到目前为止尝试了什么,当我单击任一文本框时,第二个段始终是默认选定项,因此在第一个文本字段中它有点笨拙,因为我必须单击两个索引然后它才能开始正常工作。在我这样做之后,它就可以完美地工作,但这只是最初的显示给我带来了问题。