我有一个父视图控制器和两个容器,如下所示:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyCombinationsViewController *myComb = [storyboard instantiateViewControllerWithIdentifier:@"MyCombinationsViewControllerID"];
myComb.view.frame = self.combinationsContainer.bounds;
[self.combinationsContainer addSubview:myComb.view];
[self addChildViewController:myComb];
[myComb didMoveToParentViewController:self];
StatInfoViewController *stat = [storyboard instantiateViewControllerWithIdentifier:@"StatInfoViewControllerID"];
stat.view.frame = self.statContainer.bounds;
[self.statContainer addSubview:stat.view];
[self addChildViewController:stat];
[stat didMoveToParentViewController:self];
我想在StatInfoViewController
内做一些影响MyCombinationsViewController
的事情。如何在不重新分配的情况下访问MyCombinationsViewController属性?
这样做:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyCombinationsViewController *myComb = [storyboard instantiateViewControllerWithIdentifier:@"MyCombinationsViewControllerID"];
当我尝试执行StatInfoViewController
之类的操作时,{p}内的{p>会返回空属性
答案 0 :(得分:0)
你可以在StatInfoViewController add:
的标题中链接它们togehter@property (nonatomic, weak) MyCombinationsViewController* myCombinationsViewController
然后在分配它们时,请执行以下操作:
StatInfoViewController *stat = [storyboard instantiateViewControllerWithIdentifier:@"StatInfoViewControllerID"];
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyCombinationsViewController *myComb = [storyboard instantiateViewControllerWithIdentifier:@"MyCombinationsViewControllerID"];
stat.myCombinationsViewController = myComb;
myComb.view.frame = self.combinationsContainer.bounds;
[self.combinationsContainer addSubview:myComb.view];
[self addChildViewController:myComb];
[myComb didMoveToParentViewController:self];
stat.view.frame = self.statContainer.bounds;
[self.statContainer addSubview:stat.view];
[self addChildViewController:stat];
[stat didMoveToParentViewController:self];
然后你可以从StatInfoViewController里面访问myCombinationsViewController