将UIViewcontrollers添加到UIScrollview

Zac*_*ach 4 iphone objective-c uitableview uiscrollview ios

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

此代码添加到Viewcontroller的viewDidLoad中,该视图包含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);
Run Code Online (Sandbox Code Playgroud)

它加载所有视图都很好.但是只有viewDidLoad在每个viewcontroller中被调用.没有其他方法被调用而且有些方法中有UItableviews.但它只展示了第一个细胞.

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

谢谢

Ale*_*lex 7

我相信您的问题是您没有将这些视图控制器添加为包含您的scrollview的控制器的子项.在Apple的View Controller编程指南中,他们提供了添加子控制器的示例:

[self addChildViewController:content];
content.view.frame = [self frameForContentController];
[self.view addSubview:self.currentClientView];
[content didMoveToParentViewController:self];
Run Code Online (Sandbox Code Playgroud)

这一个用于删除一个:

[content willMoveToParentViewController:nil];
[content.view removeFromSuperview];
[content removeFromParentViewController];
Run Code Online (Sandbox Code Playgroud)

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

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