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

How to load the view controller inside container using button?

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

 - (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");


}

我已经按照建议更新了我的代码,但这段代码对我不起作用。

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

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

将您的第一个控制器连接到第二个、推送或模式转场,并在故事板右侧面板的“属性”部分下为转场命名。

然后在你的实现文件中,写下下面的代码

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

This link一定对你有帮助。

编辑:点击按钮调用其他两个控制器:

1) 删除按钮与 VC 的连接。

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

3) 将 ID 设置为 3 个不同的转场,转场标识符(在属性部分下,故事板的右侧面板)。

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];
}