User User - 5 months ago 17
Objective-C Question

How to click button and move on next page?

I have

UIScrollView
with
pagingEnabled=YES
.
I want to scroll in next page on button click but my code doesn’t work.
When I press on button once I move to next page but when press again I move in the end page. How to click button and move on next page?

-(IBAction)aMethod:(id)sender
{
int i;
i = i+1;

[scroller setContentOffset:CGPointMake(scroller.frame.size.width*i, 0) animated:YES];
}

Answer

The problem is that int i is defined everytime when you fire aMethod:.

You should define @property (nonatomic, assign) NSInteger currentPageIndex; in your class' interface and increment it when invoke aMethod:

- (IBAction)nextPageButtonTapped:(id)sender {
    self.currentPageIndex++;
    CGFloat pageWidth = scroller.frame.size.width;
    [scroller setContentOffset:CGPointMake(pageWidth*self.currentPageIndex, 0) animated:YES];
}
Comments