I want to display the contents of a double picker to a label without hitting a button in the process. Can anyone show me how this is accomplished.
I disp开发者_高级运维laying calculations to a label and I want them to change as the user moves the picker.
You need to set up a UIPickerViewDelegate
, set it as the delegate of your picker, and implement – pickerView:didSelectRow:inComponent:
to modify your label how you want. The UIPickerViewDelegate protocol reference is here. I would probably just set the viewController for the containing view as the delegate, but it depends on how your program is structured.
EDIT - example code added:
You declare a class as implementing a delegate protocol with <> notation as in the following:
@interface MyViewController : UIViewController<UIPickerViewDelegate>
Then you can set the delegate of your picker view instance like so:
myPicker.delegate = myViewControllerInstance;
Or, if your picker is an ivar of the viewcontroller class:
myPicker.delegate = self;
Then, inside the implementation of your MyViewController class you can optionally implement the methods of the UIPickerViewDelegate protocol, and the corresponding messages will be sent at the right time. eg, -pickerView:didSelectRow:inComponent:
(if implemented in your delegate) will be called whenever the picker selects a value, and it will be passed the row selected and the component the row was selected in.
精彩评论