Reputation: 961
ViewController
which contains elements such as
UIPickerView
and two UITextField
s.PickerViewController
, where UIPickerViewDelegate
and UIPickerViewDataSource
methods are implemented (didSelectRow
, titleForRow
, and the other ones).After an element is selected in the UIPickerView
, I need to change the text of one of the UITextField
s. I can't do that, because didSelectRow:
is implemented in PickerViewController
but UITextField
is contained in ViewController
. How can I get access? And most importantly, how can I do that correctly?
Should I implement the picker methods in another class, or have them in ViewController
?
Upvotes: 1
Views: 74
Reputation: 64002
how can I do that correctly?
This is a better question than simply how to let the PickerViewController
change a text field in its parent view controller, because really it shouldn't.
Two possibilities spring to mind: the picker controller can pass some kind of message directly back to the other controller, or it can update a model, which the other controller reads from after the picker view is done.
The first is probably most easily accomplished by giving the picker controller an "after picking" handler Block that would have the necessary data as an argument, and that the parent controller defines and sets on the other. Before the picker controller is dismissed, it should call the Block, passing in the selection and performing whatever actions the other controller specified.
The second means that the parent controller and the picker controller must have access to the same data object. Without knowing what else is going on, it's hard to be exact, but one good possibility is that the parent controller actually gives the picker controller the model object that it should read from, rather than the picker controller acquiring it on its own. The picker controller just changes the object that it's passed.
Upvotes: 1
Reputation: 12303
You can add a UITextField properties to a PickerViewController and pass UITexiField instances from UIViewController:
in PickerViewController.h:
@property UITextField *textField1, *textField2;
in UIViewController.m:
PickerViewController *picker = [PickerViewController new];
picker.textField1 = self.textField1;
picker.textField2 = self.textField2;
You can implement PickerView anyway - in another class or in ViewController.
Upvotes: 0