ios - UIPageViewController programmatically tell what page is showing -
is there way programmatically tell when page showing in pageviewcontroller? example, instantiated following:
if (index == 0) { pagecontentviewcontroller = [storyboard instantiateviewcontrollerwithidentifier:@"oncampustable"]; } else if (index == 1) { pagecontentviewcontroller = [storyboard instantiateviewcontrollerwithidentifier:@"offcampustable"]; } else if (index == 2) { pagecontentviewcontroller = [storyboard instantiateviewcontrollerwithidentifier:@"myeventstable"]; }
i want write
if (current page "oncampustable") { method 1 } else if (current page "offcampustable") { method 2 } else if (current page "myeventstable") { method 3 }
i've tried using index, because of way pages load , stuff, doesn't work properly. thinking of trying access page indicator thing (the little circle things) page number, don't know how that.
i've seen few solutions out there use array of viewcontrollers , comparisons, don't want keep bunch of controllers around (i generate them on fly model), found solution seems work.
basically point property prior , next controllers in data source before , after methods. in delegate's didfinishanimating call compare new controller these properties see if moved forward or backward. 1 caveat before , after don't called if controller has been loaded, reassign property based on didfinishanimating's previousviewcontroller's value.
note, implemented case 1 page displayed.
- (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerbeforeviewcontroller:(uiviewcontroller *)viewcontroller { if (self.pageindex > 0) { uiviewcontroller *vc = [self gencontroller:self.tracklist[self.pageindex - 1]]; self.priorvc = vc; return vc; } else { self.priorvc = nil; return nil; } } - (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerafterviewcontroller:(uiviewcontroller *)viewcontroller { if (self.pageindex < self.tracklist.count - 1) { uiviewcontroller *vc = [self gencontroller:self.tracklist[self.pageindex + 1]]; self.nextvc = vc; return vc; } else { self.nextvc = nil; return nil; } } - (void)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller didfinishanimating:(bool)finished previousviewcontrollers:(nsarray *)previousviewcontrollers transitioncompleted:(bool)completed { if (finished && completed) { if (pageviewcontroller.viewcontrollers.lastobject == self.priorvc) { nslog(@"back"); self.pageindex--; self.nextvc = previousviewcontrollers.lastobject; } else if (pageviewcontroller.viewcontrollers.lastobject == self.nextvc) { nslog(@"forward"); self.pageindex++; self.priorvc = previousviewcontrollers.lastobject; } nslog(@"page: %ld",self.pageindex); } }
Comments
Post a Comment