在 iPhone 应用程序中展开和缩回下拉列表

发布于 2024-10-26 03:59:50 字数 253 浏览 2 评论 0原文

我正在开始开发 iPhone 应用程序。我需要提供一个下拉框,供客户选择一个值,该值本质上在提交时会发送到下游数据库。我显然使用由 IB 构建的 UIPickerView 作为下拉列表。

加载视图后,下拉列表将启用,并显示所有值。

问题是:

  1. 我只能在单击下拉列表后才展开它吗?
  2. 用户选择值后我可以撤回列表吗?

我认为下拉菜单非常以网络为中心,但我可能全错了。

I'm starting off developing an iPhone app. I need to present a drop down box for the customer to pick a value which will essentially be sent downstream to a database upon submit. I'm clearly using a UIPickerView for the drop down list, built from IB.

Once my view loads, the Drop down list is enabled, with all values showing.

Question is:

  1. Can I only expand the drop down list once it's clicked?
  2. Can I retract the list once the user selects a value?

I'm thinking very web-centric in terms of drop-downs, but I could have this all wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2024-11-02 03:59:56

您有几个选择:

  1. 对于初学者来说可能最简单:推送一个新视图(作为模态视图或导航堆栈),该视图将列表显示为单击所需项目的表格视图,或显示为选择项目的 pickerView卷轴。
  2. 查看 Twitter 应用程序的“我的个人资料”视图。虽然我个人没有这样做,但我认为它只是使用 didSelectRowAtIndex 来确定用户单击的部分,然后在调用 [tableView reloadData] 之前填充该部分的值数组

You have a couple options:

  1. Probably easiest for a beginner: push a new view (either as a modal view or onto the nav stack) that presents the list either as a table view where they click the item they want or as a pickerView where it scrolls.
  2. Review the twitter app 'my profile' view. While I haven't done this personally, I think it's simply using the didSelectRowAtIndex to determine which section the user clicked and then filling an array of values for that section before calling [tableView reloadData]
醉南桥 2024-11-02 03:59:55

您的视图控制器需要实现 UIPickerViewDataSource 以及 UIPickerViewDelegate。在 UIPickerView 的 XIB 中,您需要将文件的所有者与 UIPickerView 的委托和数据源链接起来。

就像在 UITabelDataSource 中一样,您需要在 [numberOfComponentsInPickerView] 内提供组件数量(表:部分)。然后,您需要在[numberOfRowsInComponent]中提供每个组件的行数。最后,您需要为 [titleForRow] 中的每个条目提供标题。

现在,为了让一切变得生动起来,如果选择了上一个组件,您可以使用 [didSelectRow] 加载下一个组件。

注意: UIPickerView 有一个小错误,当组件填充/更改时,不会调用 [didSelectRow]。为了使其工作更顺利,我添加了一个“None”条目作为第一个条目,这是一个非选择,不会导致加载下一个组件。

这是一个基本代码:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3; // Number of Components aka Columns
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if( component == 0 ) {
        return [self.firstColumnEntries count] + 1;
    } else if( component == 1 ) {
        return [self.secondColumnEntries count] + 1;
    } else if( component == 2 ) {
        return [self.thirdColumnEntries count] + 1;
    }
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSLog( @"titleForRow(), component: %d, row: %d", component, row );
    NSDictionary *item = nil;
    if( row == 0 ) {
        return @"  ---  ";
    } else {
        int correctedRow = row - 1;
        if( component == 0 ) {
            item = [self.firstColumnEntries objectAtIndex:correctedRow];
        } else if( component == 1 ) {
            item = [self.secondColumnEntries objectAtIndex:correctedRow];
        } else if( component == 2 ) {
            item = [self.thirdColumnEntries objectAtIndex:correctedRow];
        }
        return [item objectForKey:@"name"]; // My objects are NSDictionarys
    }
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    DLog( @"SSVC.didSelectRow(), component: %d, row: %d", component, row );
    ActionRequest *action = nil;
    if( row == 0 ) {
        if( component == 0 ) {
            [self refreshFirstColumn:self.firstColumnEntries];
        } else if( component == 1 ) {
            ...
        }
    } else {
        // Last Column is selected. Now selection is complete
        ...
    }
}

因为 UIPickerView 占用了大量空间,并且您无法更改它的大小,所以我建议将其放置在另一个 UIView 上(使其余部分透明),并在用户选择分配给的字段时显示它值(UITextField)。选择完成后(选择最后一个值或用户输入按钮时),您让视图消失并将值设置到 UITextField 上。确保 UITextField 无法编辑,并且输入它会使带有选取器视图的视图出现。完成后,请确保您也移动到下一个字段。

Your view controller needs to implement the UIPickerViewDataSource as well as the UIPickerViewDelegate. In the XIB of the UIPickerView you need to link the File's Owner with the UIPickerView's delegate and datasource.

Like in the UITabelDataSource you need to provide the number of components (table: sections) inside [numberOfComponentsInPickerView]. Then you need to provide the number of rows for each component in [numberOfRowsInComponent]. Finally you need to provide the title for the each entry inside [titleForRow].

Now to bring everything to life you can use [didSelectRow] to load the next component if the previous one was selected.

Attention: UIPickerView has a little bug not to call [didSelectRow] when the component is filled / changed. In order to make it work more smoothly I added a "None" entry as the first entry which is a non-selection and does not cause the next component to be loaded.

This is a rudimentary code:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3; // Number of Components aka Columns
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if( component == 0 ) {
        return [self.firstColumnEntries count] + 1;
    } else if( component == 1 ) {
        return [self.secondColumnEntries count] + 1;
    } else if( component == 2 ) {
        return [self.thirdColumnEntries count] + 1;
    }
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSLog( @"titleForRow(), component: %d, row: %d", component, row );
    NSDictionary *item = nil;
    if( row == 0 ) {
        return @"  ---  ";
    } else {
        int correctedRow = row - 1;
        if( component == 0 ) {
            item = [self.firstColumnEntries objectAtIndex:correctedRow];
        } else if( component == 1 ) {
            item = [self.secondColumnEntries objectAtIndex:correctedRow];
        } else if( component == 2 ) {
            item = [self.thirdColumnEntries objectAtIndex:correctedRow];
        }
        return [item objectForKey:@"name"]; // My objects are NSDictionarys
    }
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    DLog( @"SSVC.didSelectRow(), component: %d, row: %d", component, row );
    ActionRequest *action = nil;
    if( row == 0 ) {
        if( component == 0 ) {
            [self refreshFirstColumn:self.firstColumnEntries];
        } else if( component == 1 ) {
            ...
        }
    } else {
        // Last Column is selected. Now selection is complete
        ...
    }
}

Because the UIPickerView takes away a lot of space and you cannot change it size I would recommend to place it on an additional UIView (make the rest transparent) and display it when the user selected the field that is assigned to the value (UITextField). When the selection is done (either when last value is selected or when the user enter a button) you let the View disappear and set the value onto the UITextField. Make sure that the UITextField cannot be edited and that entering it make the View with the Picker View appear. When done make sure that you also move to the next field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文