Adding UIViewcontrollers to UIScrollview

人盡茶涼 提交于 2019-12-21 17:58:29

问题


I am changing the layout of an existing application. There are a series of view which now needs to be added to a scrollview that the users can swipe to move to next screen. I have added the controllers to scrollview using the code below.

This code is added in the viewDidLoad of the Viewcontroller which holds the 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);

It loads all view fine. But only the viewDidLoad is getting called in each viewcontroller. No other methods are getting called And some have UItableviews in it. But its showing only the first cell.

How can I do this properly in ios?

Thanks


回答1:


I believe your problem is that you are not adding these view controllers as children of the controller that contains your scrollview. In Apple's View Controller Programming Guide they provide this example for adding a child controller:

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

and this one for removing one:

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

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

I believe this will cause the additional methods to be run.



来源:https://stackoverflow.com/questions/18897582/adding-uiviewcontrollers-to-uiscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!