Say I have a label on view1
that connects to ViewController1.m
. I want to be able to change the label.text
value within a different view controller called ViewController2.m
.
At first I tried connecting the label (already connected to ViewController1.m
) to ViewController2.m
but it doesn't allow me to connect it as an outlet, only an action. Probably because the class referenced is ViewController1
.
To directly answer your question, assuming you're storyboarding or xib'ing, you can only connect an outlet to a viewController that is the same class or a subclass as is defined in the "Custom Class" for that viewController.
BUT, and this EXTREMELY important, don't confuse connection with instantiation. Let's say you actually do connect viewController1.textField and viewController2.textField, that doesn't mean when viewController2 appears that viewController2.textField will magically have the value from viewController1.textField in it. Nor will updating viewController2.textField reflect that value back to viewController1.textField. You have to do that manually, because the connections are relative to a single given instance of a viewController.
Assuming ViewController1 is displayed first, communication in both directions follows:
If you are storyboarding, you need to pass values from one viewController to another in prepareForSegue, for example:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// NOTE: if your viewController is laid out with multiple segues,
// use [segue identifier] to discern which segue has been triggered
ViewController2 *vc = (ViewController2 *)[segue destinationViewController];
vc.textField = self.textField;
}
If you are instantiating instead of storyboarding (warning: this code hasn't been tested):
ViewController2 *vc = [[ViewController2 alloc] init];
vc.textField = self.textField;
[self presentViewController: vc animated: YES];
If you want to pass something back from viewController2 to viewController1, then you would create a protocol, set viewController1 as the delegate to viewController2, then call back as needed to mirror the value.
ViewController1.h:
#import "ViewController2.h"
@interface ViewController1 : UIViewController<ViewController2Delegate>
ViewController1.m:
vc.delegate = self; // vc = pointer to viewController2
ViewController2.h:
@protocol ViewController2Delegate<NSObject>
@required
- (void)importantValueDidChange:(NSString *)value;
@end
@property (weak, nonatomic) id<ViewController2Delegate> delegate;
ViewController2.m: (when the value needs updating)
if([self.delegate respondsToSelector: @selector(@ importantValueDidChange:)]) {
[self.delegate importantValueDidChange: self.textField.text];
}