我想将一个字符串传递给作为 rootController.view 的子视图添加的 label.text:
@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;
}
消息在另一个 ViewController 中发送...
NSArray *viewControllers = [self.navigationController viewControllers];
MainViewController *textTarget = (MainViewController *)[viewControllers objectAtIndex:0];
[textTarget updateLabel:@"Hello"];
但它永远不会到达 rootController 并且不会调用该方法。另一个 ViewController 发送所有到达的其他消息。但只是不是为 rootController 准备的。知道为什么它不起作用吗?