ios 将子视图控制器的视图添加到父视图控制器的子视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17011579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a child view controller's view to a subview of the parent view controller
提问by Mischa
I want to add a tableViewController as a child view controller of a containerViewController (shown below). According to Apple's View Controller Programming GuideI can achieve this by the following lines of code inside my containerViewController:
我想添加一个 tableViewController 作为 containerViewController 的子视图控制器(如下所示)。根据 Apple 的View Controller Programming Guide,我可以通过我的 containerViewController 中的以下几行代码来实现这一点:
[self addChildViewController:tableViewController];
[self.view addSubview:tableViewController.view];
[tableViewController didMoveToParentViewController:self];
In fact, that works fine. Now the problem is that I do not want to add the tableViewController's view as a subview of the containerViewController's root view. Instead I want to add it as a subview of the containerView (see image) which itself is a subview of the containerViewController's root view. So I changed the above code as follows:
事实上,这很好用。现在的问题是我不想将 tableViewController 的视图添加为 containerViewController 根视图的子视图。相反,我想将它添加为 containerView(参见图片)的子视图,它本身是 containerViewController 根视图的子视图。所以我把上面的代码改成如下:
[self addChildViewController:tableViewController];
[self.contentView addSubview:tableViewController.view];
[tableViewController didMoveToParentViewController:self];
Now when I launch the app it crashes immediately with this error:
现在,当我启动应用程序时,它会立即崩溃并显示以下错误:
Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller: should have parent view controller: but actual parent is:'
由于未捕获的异常“UIViewControllerHierarchyInconsistency”而终止应用程序,原因:“子视图控制器:应该有父视图控制器:但实际的父视图是:”
What is the problem here? Is it not possible to add a childViewController's view to a specific subview of its containerViewController? Or how can I achieve this without an error in the view controller hierarchy?
这里有什么问题?是否无法将 childViewController 的视图添加到其 containerViewController的特定子视图中?或者如何在视图控制器层次结构中没有错误的情况下实现这一点?
回答by Anupdas
It doesn't really matter which view you are adding the child viewController to. If a view of a viewController is added to another viewController you need set it properly.
您将子视图控制器添加到哪个视图并不重要。如果一个 viewController 的视图被添加到另一个 viewController 中,你需要正确设置它。
tableViewController.view.frame = self.contentView.bounds;
[self.contentView addSubview:tableViewController.view];
/*Calling the addChildViewController: method also calls
the child's willMoveToParentViewController: method automatically */
[self addChildViewController:tableViewController];
[tableViewController didMoveToParentViewController:self];
回答by Manjeet
//class name InfoViewController
IBOutlet UIView *addViewToAddPlot;
InfoViewController *InfoController;
-(void) add_method
{
InfoController = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:nil];
InfoController.view.frame = self.addViewToAddPlot.bounds;
[self containerAddChildViewController:InfoController];
}
-(void) remove_method
{
[self containerRemoveChildViewController : InfoController];
}
- (void)containerAddChildViewController:(UIViewController *)childViewController {
[self addChildViewController:childViewController];
[self.addViewToAddPlot addSubview:childViewController.view];
[childViewController didMoveToParentViewController:self];
}
- (void)containerRemoveChildViewController:(UIViewController *)childViewController {
[childViewController willMoveToParentViewController:nil];
[childViewController.view removeFromSuperview];
[childViewController removeFromParentViewController];
}
Add and remove viewcontroller ,#childviewcontroller
添加和删除 viewcontroller ,#childviewcontroller
回答by Ram Madhavan
To show a child_view_controller over a main_view_controller.
在 main_view_controller 上显示 child_view_controller。
step 1: create a main_view_controller in storyboard.
第 1 步:在故事板中创建一个 main_view_controller。
step 2: create a child_view_controller with a UIview and some Label insidein storyboard.
第 2 步:在故事板中创建一个带有UIview 和一些标签的child_view_controller。
step 3: in main_view_controller's button action add the following code:
第 3 步:在 main_view_controller 的按钮操作中添加以下代码:
- (IBAction)YourButtonAction:(id)sender {
ChildViewControllerName *childViewControllerName = [self.storyboard instantiateViewControllerWithIdentifier:@"storyboardIdYouProvided"];
childViewControllerName.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:childViewControllerName.view];
[childViewControllerName didMoveToParentViewController:self];
}