将UIViewcontrollers添加到UIScrollview

我正在改变现有应用程序的布局。 现在有一系列的视图需要添加到滚动视图,用户可以滑动移动到下一个屏幕。 我已经使用下面的代码将控制器添加到滚动视图。

这个代码被添加到ViewController的viewDidLoad中,该ViewController持有UIScrolliew

int i=1; int width = 0,height=0; for(POTCTask *task in [CommonData tasks]) { UIViewController<TaskViewController> *controller = [TaskViewFactory getTaskViewController:(task.inputTypeId)]; width = controller.view.frame.size.width; height = controller.view.frame.size.height; controller.view.frame = CGRectMake(width*(i-1), 0, width, height); [self.scrollView addSubview:controller.view]; i++; } self.scrollView.contentSize = CGSizeMake(width*i, height); 

它加载所有视图都很好。 但是只有viewDidLoad在每个viewcontroller中被调用。 没有其他方法被调用,而且有一些方法在其中。 但它只显示第一个单元格。

我怎么能在iOS中正确做到这一点?

谢谢

我相信你的问题是你没有添加这些视图控制器作为包含您的滚动视图的控制器的子项。 在Apple的“视图控制器编程指南”中,他们提供了用于添加子控制器的示例:

 [self addChildViewController:content]; content.view.frame = [self frameForContentController]; [self.view addSubview:self.currentClientView]; [content didMoveToParentViewController:self]; 

和这一个去除一个:

 [content willMoveToParentViewController:nil]; [content.view removeFromSuperview]; [content removeFromParentViewController]; 

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

我相信这会导致其他方法运行。