0

我有一个父视图控制器和两个容器,如下所示:

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当我尝试执行类似的操作时,内部返回空属性NSLog(@"%@",myComb.something);

4

1 回答 1

0

您可以将它们链接在一起,通过在 StatInfoViewController 的标题中添加:

@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

于 2013-08-05T09:00:08.223 回答