iOS键盘与工具栏和UISegmentedControl

时间:2012-08-17 22:14:18

标签: ios uitoolbar uikeyboard uisegmentedcontrol

我在当前iOS应用中的键盘工具栏中遇到了分段控件的问题。我试图基本上有一个带有工具栏的键盘,左侧有一个Segment控件,有Previous / Next选项,然后是Done按钮。我得到了所有设置并且所有内容都正确触发,除了我要做的一件事是确保在选择适当的文本框时预先选择正确的段。我遇到的问题是,无论我点击什么文本字段,选择了错误的段,我必须按两次才能按正确的顺序获取它。

以下是正在创建的工具栏的代码(这是在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;
}

因此,您可以看到我基本上希望它进行设置,以便在选择第一个文本框时,已经选择了段控件中的上一个段,然后是第二个文本框和下一个段的相同。但是无论我到目前为止,当我点击任一文本框时,第二段始终是默认的选定项目,所以在第一个Textfield中它有点笨重,因为我必须单击两个索引然后它才开始正常工作。在我这样做之后,它完美地工作,但它只是初始显示给我一个问题。

1 个答案:

答案 0 :(得分:0)

您无法将栏添加到两个视图中 - 它是一个对象,您无法共享它。您应该创建两个这样的栏,为两个视图添加一个唯一的栏。保持对两个分段控件的引用,并更新它们以反映所选的文本框。我相信架构会解决这个问题。如果没有,请在执行此操作后更新问题或在此答案中添加注释。