PickerView didSelectRow:inComponent: vs. Key Value Observing
I have two UIPickerView
s. W开发者_开发百科hen the user rotates one of the pickers, a few text labels should change to reflect what has been selected. Now there are imo two options of how to do it:
- Implement
didSelectRow:inComponent:
and change the labels and whatever additional views in there - Add an observer, which is triggered when the
UIPickerView
changes its value.
I have the feeling that the first option quickly leads to a messy situation, where you need a lot of cross references from delegate to the views you want to modify. Is there any reason why one should prefer option 1 over option 2?
Unfortunately, option 1 is what I see in most tutorials out there.
I prefer the delegate route because you get it for free and it is--to me--very clear when implemented.
I am not sure what you mean when you say "I have the feeling that the first option quickly leads to a messy situation, where you need a lot of cross references from delegate to the views you want to modify." the reading i have is that you have multiple views on the stack, if that is the case, why bother updating those directly when the pickerview changes? When the new view is going to appear, can't you check the model and have the view update itself?
And there is a little bit of talk about KVO's merit here: http://iphonedevelopment.blogspot.com/2009/02/kvo-and-iphone-sdk.html
精彩评论