我试图弄清楚为什么事情会按照代码中描述的方式发生。我可以理解,如果aComb.someArray
尚未分配,instantiateViewControllerWithIdentifier
它将返回 NULL,但如果我设置计时器,数据将神奇地出现在那里。我想要一个解释和一个想法,如何在不使用丑陋计时器的情况下避免这种情况。
视图控制器A:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewControllerB *aComb = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerBID"];
aComb.view.frame = self.combinationsContainer.bounds;
aComb.someArray = _someArray;
[self.combinationsContainer addSubview:aComb.view];
[self addChildViewController:aComb];
[aComb didMoveToParentViewController:self];
视图控制器B:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@",_someArray); //<-- this will return NULL
}
带计时器的 ViewControllerB:
- (void)viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval: 1
target: self
selector: @selector(test)
userInfo: nil
repeats: NO];
}
-(void)test
{
NSLog(@"%@",_someArray); // <-- this will return Data
}