如何在子视图中使用addChildViewController [英] how to use addChildViewController in subview
本文介绍了如何在子视图中使用addChildViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在View
(子视图数组的一个视图)中使用addChildViewController
,但我对此一无所知.这是我的代码:
I want to use addChildViewController
in View
(one view fro subviews array) but I don't know about that. this is my code :
for (UIView *subview in self.view.subviews) {
if (subview.tag == 1) {
CartView *cart = [[CartView alloc]init];
[cart willMoveToParentViewController:????];/* (UIViewController*) from subview*/
[cart.view setFrame:CGRectMake(0.0f,CGRectGetHeight(self.view.frame),CGRectGetWidth(self.view.frame),CGRectGetHeight(self.view.frame))];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[cart.view setFrame:CGRectMake(0.0f,0,CGRectGetWidth(self.view.frame),CGRectGetHeight(self.view.frame))];
[UIView commitAnimations];
[subview addSubview:cart.view];
[???? addChildViewController:cart];
[cart didMoveToParentViewController:????];
}
}
我不知道如何从子视图中获取UIViewController * !!!
I don't know how to get UIViewController* from subview!!!!
推荐答案
摘自Apple文档:
(这里的内容被视为子控制器)
(Here content is considered as child controller)
将另一个视图控制器的视图添加到容器的视图层次结构中
Adding another view controller’s view to the container’s view hierarchy
- (void) displayContentController: (UIViewController*) content;
{
[self addChildViewController:content]; // 1
content.view.frame = [self frameForContentController]; // 2
[self.view addSubview:self.currentClientView];
[content didMoveToParentViewController:self]; // 3
}
以下是代码的作用:
- 它调用容器的addChildViewController:方法添加子项.调用addChildViewController:方法还会自动调用子级的willMoveToParentViewController:方法.
- 它访问子级的view属性以检索视图并将其添加到其自己的视图层次结构中.容器会在添加视图之前设置孩子的大小和位置;容器始终选择显示儿童内容的位置.尽管此示例通过显式设置框架来完成此操作,但您也可以使用布局约束来确定视图的位置.
- 它显式调用子级的didMoveToParentViewController:方法以表明操作已完成
所以,在您的情况下,您试图将ViewController添加到视图中不起作用. CartView
应该是UIViewController
而不是UIView
So ,in your case you are trying to add ViewController to your view which doesn't work. The CartView
should be a UIViewController
not UIView
这篇关于如何在子视图中使用addChildViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文