将UIViewcontrollers添加到UIScrollview [英] Adding UIViewcontrollers to UIScrollview

查看:61
本文介绍了将UIViewcontrollers添加到UIScrollview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

此代码添加到Viewcontroller的viewDidLoad中拥有UIScrolliew

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);

它可以很好地加载所有视图.但是只有viewDidLoad在每个viewcontroller中被调用.没有其他方法被调用,其中一些具有UItableviews.但是它只显示第一个单元格.

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.

如何在ios中正确执行此操作?

How can I do this properly in ios?

谢谢

推荐答案

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

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];

和这一个删除一个:

[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.

这篇关于将UIViewcontrollers添加到UIScrollview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆