0

I'm trying to create a UIScrollView with 3 UIViewController subviews. 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. I downloaded the sample code which the answerer nicely posted https://github.com/gneil90/CustomContainerViewController and my question is as follows:

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

Community
  • 1
  • 1
Apollo
  • 8,874
  • 32
  • 104
  • 192

1 Answers1

0

You can use UIScrollView delegate method, initialize only first view controller as you are doing. For rest of your view controller you can create property. In UIScrollView delegate method scrollViewDidScroll do following-

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

rishi
  • 11,779
  • 4
  • 40
  • 59
  • what if I want to re-initialize the viewcontroller for some reason? – Apollo Jun 21 '14 at 14:31
  • For what reason re initialisation is required?? If some data needs to be changed then that can be done without it. – rishi Jun 22 '14 at 04:27