获取容器VC而不是查看

时间:2013-06-05 12:35:31

标签: ios objective-c uiviewcontroller storyboard containers

我对容器的工作原理有点困惑。我有容器: enter image description here

我尝试在MainViewController中管理这个问题。但当我控制 - 将它拖入我的.h文件时,我正在

@property (weak, nonatomic) IBOutlet UIView *liveContainer;

为什么这是一个UIView类?这意味着是自我。查看我的BaseButtonContainerView?但我有BaseButtonContainerViewController,在调试中我看到viewDidLoad被调用。我可以访问BaseButtonContainerViewController中的MainViewController方法吗?

2 个答案:

答案 0 :(得分:2)

故事板喜欢在prepareForSegue中执行所有操作,因此只需为segue分配一个标识符即可。在这种情况下,我们假设您在IB中将其设置为baseButtonContainerSegue。然后在prepareForSegue中使用:

- (void) prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"baseButtonContainerSegue"]) {
        self.liveContainer = segue.destinationViewController;
    } 
}

确保您拥有UIViewController属性liveContainer,(特别是在您的情况下,您希望拥有BaseButtonContainerViewController属性)。我不确定为什么你必须这样做,但我猜它与内存管理有关。假设您离开该屏幕并且您的容器已取消分配。然后,当你回去时,你可能会失去与他们的联系。这样就可以再次调用segue,你可以将它们作为属性来获取。再说一遍,我不确定,所以如果其他人确实请赐教我!

答案 1 :(得分:0)

嵌入在容器视图中的控制器是MainViewController的子视图控制器,因此您可以使用self.childViewControllers访问它们。这将为您提供所有子项的数组,因此您必须使用objectAtIndex:来访问任何一个特定的控制器。容器视图只是普通的UIViews。故事板正在为您设置子视图控制器。你得到的就像你使用自定义容器控制器api在代码中自己添加孩子一样(使用addChildViewController:和didMoveToParentViewController :,然后将子视图添加到self.view或self.view的子视图)

相关问题