开发者

get selected row from custom datasource?

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 Dat开发者_StackOverflowaSource class in the didSelectRow:(NSInteger)row inComponent:(NSInteger) method and have a getter for that value to use from my other views?


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜