从子视图控制器返回到容器 [英] Return from Child View Controller to Container

查看:39
本文介绍了从子视图控制器返回到容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

容器VC:4个按钮,可导航到4个单独的子视图控制器.

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

单击按钮后,当前视图将替换为VC子视图.因此,按钮不再可见.出于这个原因,子VC具有专门设计为返回到其中包含4个按钮的容器VC的主页按钮.

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.

4个按钮中的1个按钮的示例调用一个功能以显示子VC:

Example of 1 of 4 Buttons Calling a function to display child 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]; 

}

问题1:如何在子VC上捕获/执行功能.例如,如何获取子VC上的主页"按钮,现在导致子VC自身删除并再次显示容器/导航屏幕?

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?

问题2:这些过程在定制容器VC或子VC中在哪里进行?

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

问题3:是否特别有指南或教程来说明如何在父子关系中管理IBAction和IBOutlet的关系?

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?

推荐答案

如果您不想要任何动画,则可以像这样完成返回操作,并将代码放在父视图控制器中:

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

通常,应从自定义容器控制器中添加和删除子级.我不确定您的第三个问题是什么意思.IBAction和出口属于其中具有UI项的任何控制器视图.您的总体设计与Apple制造容器控制器的方式不同.除了导航或标签栏视图外,诸如导航和标签栏控制器之类的容器都没有其他视图可以返回到该视图,其中一种隐藏视图始终显示在屏幕上.我不知道为什么要在这种情况下使用自定义控制器,因为它的设计看起来很像标签栏控制器.

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.

这篇关于从子视图控制器返回到容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆