I’d like to pass on a string to a label.text added as a subview of rootController.view:
@implementation MainViewController //the rootController
@synthesize leftLabel;
- (void)viewDidLoad {
[super viewDidLoad];
leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 558.0, 250.0, 30.0)];
leftLabel.font = [UIFont systemFontOfSize:20.0];
leftLabel.textAlignment = UITextAlignmentCenter;
leftLabel.textColor = [UIColor whiteColor];
leftLabel.backgroundColor = [UIColor clearColor];
leftLabel.text = @"Some text";
[self.view addSubview:leftLabel];
}
- (void)updateLabel:(NSString *)aText {
leftLabel.text = aText;
}
The message is sent in another ViewController…
NSArray *viewControllers = [self.navigationController viewControllers];
MainViewController *textTarget = (MainViewController *)[viewControllers objectAtIndex:0];
[textTarget updateLabel:@"Hello"];
but it never arrives in the rootController and the method doesn’t get called. The other ViewController sends other messages which all arrive. But just not the one intended for the rootController. Any idea why it doesn’t work?
If your VC isn’t pushed to a navigation controller at the time of sending the message, then
self.navigationControllerreturns nil, and then all other methods return nil, making all calls ineffective.