开发者

How do you notify a view controller that a row has been selected in a UIPickerView?

开发者 https://www.devze.com 2023-03-11 12:43 出处:网络
How do I notify my view controller and pass it the string when a row was selected in my UIPickerView?

How do I notify my view controller and pass it the string when a row was selected in my UIPickerView?

I ha开发者_JAVA百科ve a custom UIPickerView in regular view and I have a custom datasource/delegate in a different class.

How do I notify my view during the pickerView:didSelectRow:inComponent: delegate method? and pass the selected string to the view controller?


The class that is conforming to the picker delegates would implement

pickerView:didSelectRow:inComponent:

to know which row (or string) was selected in the picker.

If some other class from the outside world wants to know which row was selected, then this class in turn needs to inform. This will create a chain of delegation.

For example, if pickerClass is the class that implements the delegate method. If some other class (say outsideClass) wants to know about which row was selected, then you would make this outsideClass delegate of pickerClass and pass on this message to it.

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[_pickerClassDelegate pickerClass:self didSelectRow:row inComponent:component];
}


For that, you have to create one method in custom class like below:

.h file id parent;

.m file

 -(void)initWithPageNumber:(id)pidParent{
      parent=pidParent
}

Then you have set the delegate method programatically like below: uipickerview.delegate = parent; uipickerview.datasource = parent;

Hope it will be helpful.

Let me know in case of difficulty.


You can do this by either using posting and observing notification using NSNOtification centet or by creating a delegate protocol. Study delegate protocols that is a good thing to learn in objective-c.

0

精彩评论

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