从另一个Container视图控制器访问Container视图控制器

时间:2013-08-05 08:45:53

标签: ios objective-c cocoa-touch

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

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>会返回空属性

1 个答案:

答案 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