简体   繁体   中英

UIPageControl UIControlEventValueChanged - how do I know previous value?

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM