简体   繁体   English

具有3个UIViewController子视图的UIScrollView

[英]UIScrollView with 3 UIViewController subviews

I'm trying to create a UIScrollView with 3 UIViewController subviews. 我正在尝试创建具有3个UIViewController子视图的UIScrollView。 I'm done some research and it seems that the answer to this question provides a good solution Setting up UIScrollView to swipe between 3 view controllers , but I'm not sure. 我已经做过一些研究,看来这个问题的答案提供了一个不错的解决方案。 设置UIScrollView以便在3个视图控制器之间滑动 ,但是我不确定。 I downloaded the sample code which the answerer nicely posted https://github.com/gneil90/CustomContainerViewController and my question is as follows: 我下载了示例代码,回答者将其很好地发布在https://github.com/gneil90/CustomContainerViewController上 ,我的问题如下:

BViewController *bViewController = [[BViewController alloc]init];
[self addChildViewController:bViewController];
[self.scrollView addSubview:bViewController.view];
[bViewController didMoveToParentViewController:self];

CViewController *cViewController = [[CViewController alloc]init];
CGRect frame = cViewController.view.frame;
frame.origin.x = 320;
cViewController.view.frame = frame;

[self addChildViewController:cViewController];
[self.scrollView addSubview:cViewController.view];
[cViewController didMoveToParentViewController:self];

self.scrollView.contentSize = CGSizeMake(640, self.view.frame.size.height);
self.scrollView.pagingEnabled = YES;

The code in the sample project initializes both UIViewControllers at once. 示例项目中的代码立即初始化两个UIViewControllers。 Is there any way to lazy load them/would this provide any kind of performance enhancements? 有什么办法可以延迟加载它们/是否可以提供某种形式的性能增强? If 2 of my viewcontrollers download data, I wouldn't want to download this data unless the user was actually viewing these screens, but with this kind of initialization it would seems as though the user quickly viewed both of them. 如果我的两个视图控制器下载了数据,除非用户实际上正在查看这些屏幕,否则我不希望下载这些数据,但是通过这种初始化,用户似乎似乎很快就在浏览这两个屏幕。

You can use UIScrollView delegate method, initialize only first view controller as you are doing. 您可以使用UIScrollView委托方法,在执行操作时仅初始化第一个视图控制器。 For rest of your view controller you can create property. 对于其余的视图控制器,您可以创建属性。 In UIScrollView delegate method scrollViewDidScroll do following- UIScrollView委托方法scrollViewDidScroll请执行以下操作-

// Check at what index, to second VC or third VC
if (some condition, you can define this based on scroll postion) {

// Check if second view controller
    if (!self.cViewController) {
        cViewController = [[CViewController alloc]init];
        CGRect frame = cViewController.view.frame;
        frame.origin.x = 320;
        cViewController.view.frame = frame;
    }
// similarly for third 
}

Using this view controller are initialized as they are needed. 使用此视图控制器时,将根据需要对其进行初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM