【问题标题】:iOS: Method in rootController doesn't get callediOS:rootController 中的方法没有被调用
【发布时间】: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


【解决方案1】:

如果在发送消息时你的 VC 没有被推送到导航控制器,那么self.navigationController 返回 nil,然后所有其他方法返回 nil,使所有调用无效。

【讨论】:

    猜你喜欢
    • 2012-03-13
    • 2014-06-02
    • 2011-08-28
    • 2017-02-24
    • 2015-02-23
    • 2013-12-19
    • 1970-01-01
    • 2020-09-19
    相关资源
    最近更新 更多