ios - UIPageViewController and removing current view controller -
i setup uipageviewcontroller forward , backward through view controllers . have problem when view controller adds uipageviewcontroller memory usage increase until compiler gives me received memory warning , app runs until app empties memory , works fine again . how can navigate through view controllers without increasing memory ? or better how can remove current view controller when new on adds . here code :
// create page view controller. self.pageviewcontroller = [self.storyboard instantiateviewcontrollerwithidentifier:@"pageviewcontroller"]; self.pageviewcontroller.datasource = self; //disabling tap , swipe guesture (uigesturerecognizer *recognizer in self.pageviewcontroller.gesturerecognizers) { recognizer.enabled = no; } // instantiate first view controller. uiviewcontroller *startingviewcontroller = [self viewcontrolleratindex:0]; [self.pageviewcontroller setviewcontrollers:@[startingviewcontroller] direction:uipageviewcontrollernavigationdirectionforward animated:no completion:^(bool finished) { // completion code }]; // add page view controller root view controller. [self addchildviewcontroller:self.pageviewcontroller]; [self.view addsubview:self.pageviewcontroller.view]; [self.pageviewcontroller didmovetoparentviewcontroller:self]; forward , backward actions :
- (void)gotopreviouscontentviewcontroller { // index of current view controller uiviewcontroller *currentviewcontroller = [self.pageviewcontroller.viewcontrollers objectatindex:0]; nsstring *vcrestorationid = currentviewcontroller.restorationidentifier; nsuinteger index = [self.contentpagerestorationids indexofobject:vcrestorationid]; uiviewcontroller *previousviewcontroller = [self viewcontrolleratindex:index - 1]; [self.pageviewcontroller setviewcontrollers:@[previousviewcontroller] direction:uipageviewcontrollernavigationdirectionreverse animated:no completion:^(bool finished) { // completion code }]; } more codes :
#pragma mark - uipageviewcontrollerdatasource - (nsinteger)presentationcountforpageviewcontroller:(uipageviewcontroller *)pageviewcontroller { return self.contentpagerestorationids.count; } - (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerbeforeviewcontroller:(uiviewcontroller *)viewcontroller { nsstring *vcrestorationid = viewcontroller.restorationidentifier; nsuinteger index = [self.contentpagerestorationids indexofobject:vcrestorationid]; if (index == 0) { return nil; } return [self viewcontrolleratindex:index - 1]; } - (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerafterviewcontroller:(uiviewcontroller *)viewcontroller { nsstring *vcrestorationid = viewcontroller.restorationidentifier; nsuinteger index = [self.contentpagerestorationids indexofobject:vcrestorationid]; if (index == self.contentpagerestorationids.count - 1) { return nil; } return [self viewcontrolleratindex:index + 1]; } #pragma mark - private methods - (uiviewcontroller *)viewcontrolleratindex:(nsuinteger)index { // process valid index request. if (index >= self.contentpagerestorationids.count) { return nil; } // create new view controller. basecontentviewcontroller *contentviewcontroller = (basecontentviewcontroller *)[self.storyboard instantiateviewcontrollerwithidentifier:self.contentpagerestorationids[index]]; // set data needed vc here contentviewcontroller.rootviewcontroller = self; return contentviewcontroller; }
to answer questions
"how can navigate through view controllers without increasing memory?":
you can't that. when load new view controller load of views , view controller stored in memory. can't load , display view controller not in memory.
"how can remove current view controller when new on adds":
you shouldn't that. empty view controller doesn't have big memory footprint, can have 20+ view controllers in navigation stack without memory problem. although possible implement this, takes lot of work, , doesn't solve root of problem.
what problem then?
you have memory management problem. not caused code have posted, looks fine. problem must in part of code.
there 2 common cases cause memory management issues:
retain cycles: retain cycle when 2 objects retain each other. goes against standard rules of object ownership, leaving both objects no authority release other, causing memory leak (the numbers retain count):
unnecessary caching: if downloading lot of different images , caching them, not ideal use case caching. caching ideal storing accessed objects. if not accessing these images or receive didreceivememorywarning message, should release these objects.
how can debug memory issues
the first , simple thing override view controller's dealloc method. overriding dealloc method on viewcontroller ensure viewcontroller being deallocated when expect be.
-(void)dealloc { nslog(@"viewcontroller being deallocated"); } if doesn't can try isolate problem , debugging xcode instruments. this article start you.
Comments
Post a Comment