我正在试图弄清楚为什么事情会像代码中描述的那样发生。
我可以理解,如果在aComb.someArray
的时间内尚未分配instantiateViewControllerWithIdentifier
,它将返回NULL,但如果我将设置一个计时器,数据将神奇地出现在那里。我喜欢一个解释和想法,如何在不使用丑陋计时器的情况下避免这种情况。
ViewControllerA:
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];
ViewControllerB:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@",_someArray); //<-- this will return NULL
}
ViewControllerB with timer:
- (void)viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval: 1
target: self
selector: @selector(test)
userInfo: nil
repeats: NO];
}
-(void)test
{
NSLog(@"%@",_someArray); // <-- this will return Data
}
答案 0 :(得分:0)
当您调用aComb.view.frame = self.combinationsContainer.bounds;
时,它会触发ViewControllerB的didLoad方法。但是当你的数组没有被分配时,它返回Nil
。但是当计时器触发数组得到它的值。
解决此问题。在设置帧之前分配数组。我的意思是,
aComb.someArray = _someArray;
aComb.view.frame = self.combinationsContainer.bounds;