多个 UIPickerView 和数据源

发布于 2024-12-23 20:55:16 字数 265 浏览 1 评论 0原文

我有两个 UIPickerViews 需要转到不同的数据源。我能找到的最接近的答案是: 多个 UIPickerViews

但我不知道如何执行以下操作:

您可以创建两个类 - 一个数据源对于每个选择器视图,并在 viewDidLoad 方法中手动将它们分配给选择器视图实例

似乎很简单,但代码确实使用了示例。

I have two UIPickerViews that need to go to different datasouces. The closest answer I could find was this:
Multiple UIPickerViews

But I can't figure out how to do the follwoing:

you can create two classes - one data source for each picker view, and manually assign them to the picker view instances in the viewDidLoad method

Seems easy, but code really use an example.

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

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

发布评论

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

评论(1

一口甜 2024-12-30 20:55:16
  1. 您需要为数据源创建两个类。它们显然应该派生自 NSObject 并实现 UIPickerViewDataSource 协议。

  2. 现在您应该将这些类绑定到您的UIPickerView。最简单的解决方案是将每个数据源的实例变量添加到 UIViewController 中,初始化它们并分配给 UIPickerViewdataSource 属性。

    @interfaceMyViewController
    {
        ...
        MyDataSource1 *dataSource1;
        MyDataSource2 *dataSource2;
    }
    
    ...
    
    @实现MyViewController
    - (id) initWith...
    {
        ...
        dataSource1 = [[MyDataSource1 alloc] initWithSmth:smth];
        dataSource2 = [[MyDataSource2 alloc] initWithSmthElse:smthElse];
        返回自我;
    }
    
    - (无效)viewDidLoad
    {
        ...
    
        myPickerView1Outlet.dataSource = dataSource1;
        myPickerView2Outlet.dataSource = dataSource2;
    }
    
  1. You need to create two classes for your data sources. They obviously should be derived from NSObject and implement UIPickerViewDataSource protocol.

  2. Now you should bound these classes to your UIPickerView. The easiest solution is to add instance variables for each data source to UIViewController, initialize them and assign to dataSource property of UIPickerView.

    @interface MyViewController
    {
        ...
        MyDataSource1 *dataSource1;
        MyDataSource2 *dataSource2;
    }
    
    ...
    
    @implementation MyViewController
    - (id) initWith...
    {
        ...
        dataSource1 = [[MyDataSource1 alloc] initWithSmth:smth];
        dataSource2 = [[MyDataSource2 alloc] initWithSmthElse:smthElse];
        return self;
    }
    
    - (void) viewDidLoad
    {
        ...
    
        myPickerView1Outlet.dataSource = dataSource1;
        myPickerView2Outlet.dataSource = dataSource2;
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文