【发布时间】:2012-09-02 02:05:54
【问题描述】:
我想将一个字符串传递给作为 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 设计的。知道为什么它不起作用吗?
【问题讨论】:
-
如果你的VC在发送消息的时候没有推送到导航控制器,那么
self.navigationController返回nil,然后所有其他方法返回nil,使得所有调用无效。 -
就是这样,非常感谢!
标签: ios methods uiviewcontroller uilabel