I am working on iPad ebook type application. I have to get the data from web service and display it in webView at the start of the application. so at the start of the application i am creating an array of UIViewController with UIWebview .when i am start to traverse pages from one to another at 38-42 page it receive memory warning and crash in only iPad3 but works fine in iPad2.

Below is the code for that.


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 pageContent = [[NSMutableArray alloc] init];
 for (int i=0;i<60; i++)
 {
[self addwebviewINCode]; } NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey: UIPageViewControllerOptionSpineLocationKey]; self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options: options]; pageController.dataSource = self; pageController.delegate=self; [[pageController view] setFrame:[[self.window.rootViewController view] bounds]]; UIViewController *viewC = [self viewControllerAtIndex:0];

NSArray *viewControllers =  [NSArray arrayWithObject:viewC];
[pageController setViewControllers:viewControllers  
                         direction:UIPageViewControllerNavigationDirectionForward 
                          animated:NO 
                        completion:nil];

[self.window.rootViewController addChildViewController:pageController];
[[self.window.rootViewController view] addSubview:[pageController view]];
[pageController didMoveToParentViewController:self.window.rootViewController];

[self.window makeKeyAndVisible];

return YES;

}

I am adding Webview with UIViewController


-(void)addwebviewINCode
{
 UIViewController *viewC = [[UIViewController  alloc] initWithNibName:nil bundle:nil];

self.webVCustom = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, _window.bounds.size.width, 1004)];//20

self.webVCustom.suppressesIncrementalRendering=YES;
self.webVCustom.delegate=self;
self.webVCustom.scrollView.scrollEnabled=NO;

viewC.view=self.webVCustom;

[pageContent addObject:viewC];

}

UIPageController Delegate methods


- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers {
    pageAnimationFinished = YES;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
   if (flag == FALSE) {
        NSUInteger index = [self indexOfViewController:viewController];
        if ((index == 0) || (index == NSNotFound)) {
            return nil;
        }
        index--;
        return [self viewControllerAtIndex:index];
    }else {
        flag=FALSE;
    }
    return 0;
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
   if (flag == FALSE) {
        NSUInteger index = [self indexOfViewController:viewController];
        if (index == NSNotFound) {
            return nil;
        }
        index++;
        if (index == [self.pageContent count]) {
            return nil;
        }
        return [self viewControllerAtIndex:index];
    }else {
        flag=FALSE;
    }
    return 0;
}

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed { UIViewController *viewC = [previousViewControllers objectAtIndex:0]; NSInteger ind = [self getVCIndex:viewC]; NSInteger indCur = [self getVCIndex:[pageViewController.viewControllers objectAtIndex:0]]; if (ind == indCur) { indCurrentView=ind; }else { indCurrentView=indCur; } if (completed || finished) // Turn is either finished or aborted pageAnimationFinished = NO; }

-(UIViewController *)viewControllerAtIndex:(NSUInteger)index { // Return the data view controller for the given index. if ((index >= [self.pageContent count])) { return nil; } // Create a new view controller and pass suitable data. UIViewController viewC = (UIViewController)[pageContent objectAtIndex:index];

return viewC;

}

-(NSUInteger)indexOfViewController:(UIViewController *)viewController { return [self.pageContent indexOfObject:viewController]; }

-(NSInteger)getVCIndex:(UIViewController*)viewCon { if ([pageContent containsObject:viewCon]) { return [pageContent indexOfObject:viewCon]; } return 0; }

Why this thing should happen because its working fine in iPad2 but give memory warning and crashes only in iPad3

can anyone help me out to solve this issue? i Have spent too many days to solve this problem..

Thanks

1

There are 1 best solutions below

0
On

It looks like the referenced code generates all of the view controllers up front. You should consider creating the view controllers only as the UIPageViewController requests them.

For the initial setViewControllers:direction:animated:completion: call, send it an array with only the view controller that should be displayed initially (the first page).

In pageViewController:viewControllerBeforeViewController: and pageViewController:viewControllerAfterViewController: it would create a new view controller representing the content that should be displayed before or after the content contained in the view controller passed in the parameter rather than referencing an index into an array.

This way you don't need to have all of the pages loaded at once which could cause problems (especially with UIWebViews).