从自定义数据源获取选定的行?
我有显示自定义 UIPickerView 的视图。我还有一个单独的 UIPickerView 数据源类,它也是 UIPicker 的委托。
我想您需要从数据源(模型)而不是选择器本身获取选定的值。
从另一个视图获取自定义 UIPickerView 的选定值的最佳方法是什么?
我是否应该将值保存在 didSelectRow:(NSInteger)row inComponent:(NSInteger) 方法中自定义 DataSource 类的本地字段中,并为该值提供一个 getter 以从其他视图中使用?
I have view that displays a custom UIPickerView. I also have a separate DataSource class for the UIPickerView that is also the delegate for the UIPicker.
I suppose you need to get selected values from the data source (the model) and not the picker itself.
What is the best way to get the selected value of the custom UIPickerView from another view?
Should I save the value in a local field in the custom DataSource class in the didSelectRow:(NSInteger)row inComponent:(NSInteger) method and have a getter for that value to use from my other views?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的问题来看,您似乎没有将代码与数据分开,换句话说,您没有遵守模型-视图-控制范例。因此,听起来您有一个控制数据的视图......但现在您需要另一个视图,事情变得一团糟。
共享数据的方法有多种,它们实际上都取决于您用它做什么。例如,假设一个视图创建数据对象并将它们存储在 NSMutableArray 或 NSMutableDictionary 中。然后,您可以传递该对象并让多个视图访问数据。
有些人可能喜欢将其创建为应用程序委托的属性。然后使用 [[UISharedApplication] delegate] 获取属性就很简单了。
我倾向于传递对象(除非使用 Core Data)并使用属性来获取和设置值。
如果答案有点模糊,我很抱歉,但问题也有点模糊 - 没有一种“正确”的方式来传递数据。有些比其他的更麻烦——你会立即知道那些是完全错误的(你最终会与代码作斗争)。
From your question, it appears that you have not separated your code from your data, in other words, your are not adhering to a Model-View-Control paradigm. As such, it sounds like you have a ONE view that is controlling your data... but now you need another view and things are getting messy.
There are multiple ways to share data and they all really depend on what you are doing with it. For example, let's say that one view creates the data objects and stores them in an NSMutableArray or an NSMutableDictionary. You can then pass THAT object around and have multiple views access the data.
Some people may like to create this as a property of the application delegate. Then it is a simple matter of using [[UISharedApplication] delegate] to get the property.
I tend to prefer to pass objects around (unless using Core Data) and use properties to get and set values.
I'm sorry if the answer is a little vague, but the question was kinda vague too - there isn't one "right" way to pass data around. There are some that are more cumbersome than others - and you will know right away the ones that are dead wrong (you'll end up fighting the code).