【问题标题】:iOS Keyboard with Toolbar and UISegmentedControl带有工具栏和 UISegmentedControl 的 iOS 键盘
【发布时间】:2012-08-18 06:40:09
【问题描述】:

在我当前的 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;
}

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

【问题讨论】:

    标签: ios uitoolbar uikeyboard uisegmentedcontrol


    【解决方案1】:

    您不能将栏添加到两个视图 - 它是一个对象,您不能共享它。您应该创建两个这样的栏,为两个视图添加一个唯一的。保留对两个分段控件的引用,并更新两者以反映选定的文本框。我相信架构会解决这个问题。如果没有,请在完成后更新问题或对此答案添加评论。

    【讨论】:

    • 谢谢,看来这就是问题所在。所以最后我必须创建 2 个工具栏和 2 个段控件(每个 TextField 1 个),然后我还需要确保在 SegementControl Selected Segment 操作中,只需将两个段控件设置为选定的任何一个的索引。没想到我必须制作 2,但我想这是让它发挥作用的最佳方式。谢谢。
    • 问题是视图只能在一个地方或另一个地方,当你有两个文本对象在任何时候争夺谁拥有它时,就会出现问题。您可能会找到一个更复杂的解决方案,让您保留一个,但实际上,我给您的解决方案是最强大且易于维护的。
    猜你喜欢
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多