如何使用按钮加载容器内的视图控制器?

时间:2015-07-24 11:33:26

标签: ios objective-c uiview uiviewcontroller uicontainerview

这是我一直在使用的代码,当我单击按钮时,我的视图控制器中没有任何操作。

 - (IBAction)controlleraction:(id)sender {

    ChildViewController *child=[[ChildViewController alloc]init];

   rootviewController *firstview= [[rootviewControllerone alloc]init];
    [self addChildViewController:child];
    child.view.frame = self.container.frame;//container is a container view (uiview)
    [self.container addSubview:child.view];
    [firstview removeFromParentViewController];
    [child didMoveToParentViewController:self];
         NSLog(@"working");


}

我根据建议更新了我的代码,此代码对我不起作用。

enter image description here 当我单击按钮时,容器视图控制器必须加载到我的容器视图中。

1 个答案:

答案 0 :(得分:1)

在按钮上加载新的视图控制器单击执行以下操作:

将您的第一个控制器连接到第二个,推送或模态segue,并在故事板的右侧面板的属性部分下为segue指定一些名称。

然后在您的实现文件中,编写以下代码

- (IBAction)controlleraction:(id)sender
{
   [self performSegueWithIdentifier:@"SegueIdentifier" sender:nil];
}

This link必须对您有所帮助。

编辑:点击按钮调用另外两个控制器:

1)删除VC连接按钮。

2)将所有三个控制器(带黄色,紫色和黑色背景的VC)连接到第一个控制器(带按钮的VC)

3)将ID设置为3个不同的segues,Segue Identifier(在故事板的右侧面板的Attribute部分下)。

4)然后以编程方式点击按钮调用不同的VC:

- (IBAction)controlleraction:(id)sender
{
   if (CONDITION FOR 1ST VC)
      [self performSegueWithIdentifier:@"SegueIdentifier1" sender:nil];
   else if (CONDITION FOR 2nd VC)
      [self performSegueWithIdentifier:@"SegueIdentifier2" sender:nil];
   else if (CONDITION FOR 3rd VC)
      [self performSegueWithIdentifier:@"SegueIdentifier3" sender:nil];
}