【问题标题】:Multiple UIPickerViews多个 UIPickerViews
【发布时间】:2011-07-29 14:08:08
【问题描述】:

我有一个两难选择,我有两个 UIPickerViews,当两个不同的视图加载时会“显示”。我从一个UIPickerView 开始,然后通过加载数组和 ViewController 中的所有其他UIPickerView 操作来启动并运行它。

我认为这就像复制/粘贴新 UIPickerView 的相同方法一样简单,但只需更改变量名称,也在同一个 UIViewController 内。

问题是 - 两个UIPickerView 都在下拉列表中显示相同的数据集?我只是在这里遗漏了一些明显的东西吗?

以下更新

我对以下所有四种方法都这样做了,现在一旦加载“下一个”视图,应用程序就会崩溃?这看起来对吗?

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView 

- (NSInteger)thePickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component

- (NSString *)thePickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

- (void)thePickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

【问题讨论】:

  • 是的,这些方法看起来不错,您的崩溃是由于其他原因。试试调试器!在运行->调试器。或发布更多代码。

标签: iphone ios dataset uipickerview uipicker


【解决方案1】:

是的,您必须使用 UIPickerView 参数来确定哪个选择器视图实际上在请求数据 - 因为您已将两个选择器设置为相同的数据源。

-(void)numberOfComponentInPickerView:(UIPickerView*)thePickerView {
    if(thePickerView == firstPickerView) {
       // return number of components in first picker    
    } else if(thePickerView == secondPickerView) {    
       // return number of components in second picker    
    } else {
       assert(NO);
    }
}

// repeat for all other delegate / datasource methods

【讨论】:

  • Lol- 是的,在格式化方面遇到了困难 - 给我一分钟!
  • 已更新,谢谢 我想知道是否需要初始化“thePickerView”?如果是这样 - 例如,我应该将 IB 中的 UIPickerViews 与“thePickerView”和“secondPickerView”相关联吗?
  • 不,应该设置 thePickerView - 它是请求数据的选择器视图 - 没有选择器视图,不请求数据。将您的选择器视图设置为 iBOutlets 并在 xib 中连接它们。如果您仍然遇到问题,您可以创建两个类 - 每个选择器视图一个数据源,并在 viewDidLoad 方法中手动将它们分配给选择器视图实例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多