how to scroll between view controllers using page control

1.6k views Asked by At

screenshots

I want to scroll or move to another views. How to do that with use of page control?

3

There are 3 answers

2
Arjun Ker On

pages is an array where you will store storyboard id's and NSInteger currentPageIndex, set currentPageIndex = 0

- (void)viewDidLoad
{
self.pages = [[NSMutableArray alloc]init];

// instantiate the view controlles from the storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController* ViewController1 = [storyboard instantiateViewControllerWithIdentifier:@"VC1"];

UIViewController* ViewController2 = [storyboard instantiateViewControllerWithIdentifier:@"VC2"];

// load the view controllers in our pages array
[self.pages addObjectsFromArray:@[ViewController1, ViewController2]];
// your general suff
}


 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSInteger index = [_pages indexOfObject:viewController];

if ((index == NSNotFound) || (index == 0)) {
    return nil;
}

index--;
return [_pages objectAtIndex:index];
}


- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSInteger index = [_pages indexOfObject:viewController];

if (index == NSNotFound) {
    return nil;
}
index++;

if (index == [_pages count]) {
    return nil;
}
return [_pages objectAtIndex:index];
}


-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
if (completed) {
    self.currentPageIndex = [_pages indexOfObject:[pageViewController.viewControllers lastObject]];
}
}
3
iYoung On

You can use pageControl's property currentPage

@property(nonatomic) NSInteger currentPage;            // default is 0. value pinned to 0..numberOfPages-1

It can be used as

[pageControl setCurrentPage:<logic to get your VC>];
5
looking privacy On

the system provides UIPageViewController,you can use it and UIPageControl to obtain what you think