I have a picker View declared in the ViewControllerA (in Xib file). This Xib is loaded as a custom cell in ViewControllerB's tableView. If I change the value of Picker View, How can I access this changed value in ViewControllerB.
You can access the value of the UIPickerView
by exposing a property on ViewControllerA
... If there are set circumstances when viewControllerB
needs to know the value, under the control of viewControllerB
, then viewControllerB
can check the property on viewControllerA
at those times...
However, perhaps you're asking a more general question about communication - with the specific example of ViewControllerB
needing to know about a change to the UIPickerView
that's inside ViewControllerA
...
This article on Communication Patterns is worth reading. It covers KVO, Notifications, Delegation, Blocks and Target/Action.
There's a flow-chart near the middle of the article that can help to evaluate what communication strategy to use in a given situation.
From what you've written, it sounds like you could use either KVO (key-value observing) or delegation.
I tend to use delegation in scenarios when one UIViewContoller
wants to know about changes made in another UIViewController
, e.g. when viewControllerC
presents viewControllerD
- and wants to know about changes made in viewControllerD
.
In your case, you might use a delegate method along the lines of:
- (void)pickerViewValueDidChange:(NSString*)newValue;
That delegate method would be part of an @protocol
. Something like:
@protocol ABCPickerViewDelegate
- (void)pickerViewValueDidChange:(NSString*)newValue;
@end
See Working with Protocols if this is new to you...
viewControllerB
would conform to that protocol. viewControllerA
would have a property that conforms to the protocol, something like:
@property (weak, nonatomic) id <ABCPickerViewDelegate> pickerDelegate;
Then, when the UIPickerView
value changes within viewControllerA
- it can call the delegate method... And, viewControllerB
would then know that the change has occurred and what the value is.