Note that there are some explanatory texts on larger screens.

plurals
  1. POhow can I let UIPageViewController know when my async download is complete?
    text
    copied!<p>I currently have a UIPageViewController set up in my project almost exactly like the default page-based application template. </p> <p>However, in the init method for my ModelController I am using NSURLConnection to async download data into an array (of images) that is supposed to be displayed on the PageViewController. </p> <p>That means when my root view controller goes and inits a starting view controller the resources might not be downloaded yet and then the model controller is fetching things from an empty array which crashes the app.</p> <p>How can I implement a safe way to show the images in a PageView? I was thinking of using an empty view controller with an activity indicator as the starting view controller but I don't know how I'd then let the model controller know when the download is completed so I can then update the views with the images.</p> <p><strong>my root view controller (this is the uipageviewcontroller delegate)</strong></p> <pre><code>@interface CSAPromoViewController () @property (readonly, strong, nonatomic) CSAPromoModelController *modelController; @end @implementation CSAPromoViewController @synthesize modelController = _modelController; - (void)viewDidLoad { [super viewDidLoad]; self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; self.pageViewController.delegate = self; CSAPageDataViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard]; NSArray *viewControllers = @[startingViewController]; [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; self.pageViewController.dataSource = self.modelController; [self addChildViewController:self.pageViewController]; [self.view addSubview:self.pageViewController.view]; //set page view controller's bounds CGRect pageViewRect = self.view.bounds; self.pageViewController.view.frame = pageViewRect; [self.pageViewController didMoveToParentViewController:self]; self.view.gestureRecognizers = self.pageViewController.gestureRecognizers; } </code></pre> <p><strong>my model controller (this is the data source)</strong></p> <pre><code>@interface CSAPromoModelController() @property (readonly, strong, nonatomic) NSArray *promosArray; @end @implementation CSAPromoModelController -(id)init { self = [super init]; if (self) { NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://blah.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { _promosArray = [self parseJSON:data]; }]; } return self; } - (CSAPageDataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard { // Return the data view controller for the given index. if (([self.promosArray count] == 0) || (index &gt;= [self.promosArray count] / 2)) { return nil; } // Create a new view controller and pass suitable data. CSAPageDataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:@"CSAPageDataViewController"]; dataViewController.promoOne = [self.promosArray objectAtIndex:index * 2]; dataViewController.promoTwo = [self.promosArray objectAtIndex:(index * 2) + 1]; return dataViewController; } </code></pre> <p><strong>the data view controller</strong></p> <pre><code>@implementation CSAPageDataViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.promoLabelTop.text = [self.promoOne name]; self.promoImageTop.image = [self.promoOne image]; self.promoLabelBottom.text = [self.promoTwo name]; self.promoImageBottom.image = [self.promoTwo image]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload