iphone - Unloading viewControllers from UIPageViewController -


i got uipageviewcontroller add other viewcontrollers. viewcontrollers in array viewcontrollersarray = [[nsmutablearray alloc] init]; add viewcontrollers on array this:

        [viewcontrollersarray addobject: infoviewcontroller]; 

after viewcontrollers added on array:

nsarray *initialviewcontrollers = [[nsarray alloc] initwithobjects:[viewcontrollersarray objectatindex:0], nil]; [self setviewcontrollers:initialviewcontrollers                direction:uipageviewcontrollernavigationdirectionforward                 animated:yes               completion:^(bool finished){                   nslog(@"call success");}]; 

this above code done in viewdidload uipageviewcontroller.

for loading array got functions form <uipageviewcontrollerdatasource> use this:

- (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerbeforeviewcontroller:(uiviewcontroller *)viewcontroller {     if ([viewcontrollersarray containsobject:viewcontroller]) {         nsinteger index = [viewcontrollersarray indexofobject:viewcontroller];         if (index < [viewcontrollersarray count] && index > 0) {             return [viewcontrollersarray objectatindex:(index - 1)];         }     }     return nil; }  - (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerafterviewcontroller:(uiviewcontroller *)viewcontroller {     if ([viewcontrollersarray containsobject:viewcontroller]) {         nsinteger index = [viewcontrollersarray indexofobject:viewcontroller];         if (index < [viewcontrollersarray count] - 1) {             return [viewcontrollersarray objectatindex:(index + 1)];         }     }     return nil; } 

now problem is, when swipe, next viewcontrollers gets load, etc. don't unloaded. i'm searching way unload viewcontrollers passed save memory, , when swipe reloaded.

your problem hold on view controllers in viewcontrollersarray. array holds strong reference each object add it. easiest way save memory create requested view controller on fly in both pageviewcontroller:viewcontrollerbeforeviewcontroller: , pageviewcontroller:viewcontrollerafterviewcontroller: data source methods. way, page view controller have strong reference view controller , release (dealloc) once moved off-screen.

if it's expensive create them on-the-fly, might consider keeping 3 view controllers in array: 1 displayed , 1 immediate left , immediate right.

example:

- (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pvc       viewcontrollerbeforeviewcontroller:(mygreatviewcontroller *)vc {     nsuinteger index = vc.position - 1; //custom property on vc     if (index > 0) {       return [[mygreatviewcontroller alloc] initwithposition:index];     }      return nil; }  - (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pvc        viewcontrollerbeforeviewcontroller:(mygreatviewcontroller *)vc {     nsuinteger index = vc.position + 1; //custom property on vc     if (index <= max_position) {       return [[mygreatviewcontroller alloc] initwithposition:index];     }      return nil; } 

enjoy!


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -