UIPageControl UIControlEventValueChanged - how do I know previous value?
-
14-11-2019 - |
Question
I've got a UIPageControl
object working great in my app, except that I am not handling the default 'tap to advance/decrement' behavior.
I need to:
- handle it in my app
- know what the current and previous values of the
UIPageControl
is.
I know I can capture the 'new' value on UIControlEventValueChanged
, but how would I know the 'old'? I manually setCurrentPage
in several places in my code, but saving the old state somewhere there could lead to bugs and is sort of a hack I think.
Solution
I don't think there is a property to keep the previous page, but why not define one? You can keep all the previous page numbers in an NSMutableArray
object and;
- When you are about to go to a new page, add the current one to the array before you go,
Whenever you want to come back a previous page, get the
lastObject
of you array and remove it.- (NSInteger)previousPageNumber { NSInteger pageNumber = 0; if(self.previousPagesArray.count > 0) { pageNumber = [[self.previousPagesArray lastObject] intValue]; [self.previousPagesArray removeLastObject]; } return pageNumber; }
and
-(void)goToNextPage
{
NSNumber *pageNumber = [NSNumber numberWithInt:self.pageControl.currentPage];
[self.previousPagesArray addObject:pageNumber];
//go to next page
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow