从子视图控制器返回到容器

时间:2021-09-04 22:16:28

I have a container with 4 navigation buttons, each representing 4 individual child vc. I have successfully implement the code to go from the container to the child vc using addchildviewcontroller however now I do not know how to go back.

我有一个带有4个导航按钮的容器,每个按钮代表4个独立的儿童vc。我已经使用addchildviewcontroller成功实现了从容器到子vc的代码,但是现在我不知道如何返回。

Container VC: 4 Buttons navigating to 4 separate child view controllers.

容器VC:4个按钮导航到4个独立的子视图控制器。

When the button is clicked the current view is replaced by the view of the Child VC. Therefore the buttons are no longer visible. For this very reason the child VC has a home button specifically designed to return to the container VC where the 4 buttons are residing.

单击该按钮时,当前视图将替换为子VC的视图。因此按钮不再可见。出于这个原因,子VC具有专门设计的主页按钮,以返回到4个按钮所在的容器VC。

Example of 1 of 4 Buttons Calling a function to display child VC:

4个按钮中的1个示例调用显示子VC的函数:

- (IBAction)btn_bus:(id)sender {   
   [self addMyController:businessVC_];
}

Adding Child View Controllers, function called when button is clicked:

添加子视图控制器,单击按钮时调用的函数:

-(void)addMyController:(UIViewController *)myController{
    [self addChildViewController:myController];
    [self.view addSubview:myController.view]; 
    [myController didMoveToParentViewController:self]; 

}

Question 1: How do you trap/perform functions on a child VC. For example how do I get the Home button on my Child VC to now cause the child vc to remove itself and once again show the container/nav screen?

问题1:如何在子VC上捕获/执行功能。例如,如何让我的Child VC上的Home按钮现在让孩子vc自行移除并再次显示容器/导航屏幕?

Question 2: Where are these procedures to take place in the custom container VC or child VC?

问题2:这些程序在自定义容器VC或子VC中发生在哪里?

Question 3: Is there in particular a guide or tutorial that shows how the relationship of IBAction and IBOutlet are managed in a Parent-Child Relationship?

问题3:是否特别提供了一份指南或教程,说明如何在亲子关系中管理IBAction与IBOutlet的关系?

2 个解决方案

#1


3  

If you don't want any animation, going back is done like this, with the code being in the parent view controller:

如果你不想要任何动画,那么返回就像这样,代码在父视图控制器中:

-(void)removeChild:(UIViewController *) child {
    [child didMoveToParentViewController:nil];
    [child.view removeFromSuperview];
    [child removeFromParentViewController];
}

In the child controller, you would call it like this:

在子控制器中,您可以这样调用它:

-(IBAction) goBackToContainer {
    [(ParentClassNameHere *)self.parentViewController removeChild:self];
}

In general, adding and removing children should be done from the custom container controller. I'm not sure what you mean by your third question. IBActions and outlets belong to whichever controller's view has the UI item in it. Your overall design is different than the way Apple does their container controllers. Containers like navigation and tab bar controllers don't have a view to go back to except for the navigation or tab bar views -- one of the chid views is always on screen. I don't know why you're doing a custom controller in this case, since its design seems pretty much like a tab bar controller.

通常,应从自定义容器控制器中添加和删除子项。我不确定你的第三个问题是什么意思。 IBActions和outlet属于控制器视图中包含UI项的视图。您的整体设计与Apple使用其容器控制器的方式不同。导航和标签栏控制器之类的容器除了导航或标签栏视图外没有返回的视图 - 其中一个chid视图始终在屏幕上。我不知道为什么你在这种情况下做自定义控制器,因为它的设计看起来很像一个标签栏控制器。

#2


2  

There is a little mistake in the previous answer and as I haven´t enough reputation to leave a comment. The first line should be:

在之前的回答中有一点错误,因为我没有足够的声誉来发表评论。第一行应该是:

[child willMoveToParentViewController:nil];

This tells the child that it is being removed.

这告诉孩子它正被删除。

In the Apple Docs look for Creating Custom Container View Controllers.

在Apple Docs中查找创建自定义容器视图控制器。

And UIViewController.h has a very good documentation.

而UIViewController.h有一个非常好的文档。

#1


3  

If you don't want any animation, going back is done like this, with the code being in the parent view controller:

如果你不想要任何动画,那么返回就像这样,代码在父视图控制器中:

-(void)removeChild:(UIViewController *) child {
    [child didMoveToParentViewController:nil];
    [child.view removeFromSuperview];
    [child removeFromParentViewController];
}

In the child controller, you would call it like this:

在子控制器中,您可以这样调用它:

-(IBAction) goBackToContainer {
    [(ParentClassNameHere *)self.parentViewController removeChild:self];
}

In general, adding and removing children should be done from the custom container controller. I'm not sure what you mean by your third question. IBActions and outlets belong to whichever controller's view has the UI item in it. Your overall design is different than the way Apple does their container controllers. Containers like navigation and tab bar controllers don't have a view to go back to except for the navigation or tab bar views -- one of the chid views is always on screen. I don't know why you're doing a custom controller in this case, since its design seems pretty much like a tab bar controller.

通常,应从自定义容器控制器中添加和删除子项。我不确定你的第三个问题是什么意思。 IBActions和outlet属于控制器视图中包含UI项的视图。您的整体设计与Apple使用其容器控制器的方式不同。导航和标签栏控制器之类的容器除了导航或标签栏视图外没有返回的视图 - 其中一个chid视图始终在屏幕上。我不知道为什么你在这种情况下做自定义控制器,因为它的设计看起来很像一个标签栏控制器。

#2


2  

There is a little mistake in the previous answer and as I haven´t enough reputation to leave a comment. The first line should be:

在之前的回答中有一点错误,因为我没有足够的声誉来发表评论。第一行应该是:

[child willMoveToParentViewController:nil];

This tells the child that it is being removed.

这告诉孩子它正被删除。

In the Apple Docs look for Creating Custom Container View Controllers.

在Apple Docs中查找创建自定义容器视图控制器。

And UIViewController.h has a very good documentation.

而UIViewController.h有一个非常好的文档。