Reputation: 114
I am using the following code for implementing UIPageViewController
. How to move next and previous page using UIButton click event?
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentView*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentView *) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [arrCarStatus count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
Upvotes: 2
Views: 1122
Reputation: 331
you can just use the delegate Methods of page view controller named as a
- (void)changePage:(UIPageViewControllerNavigationDirection)direction
and just simple check the condition which i mention below.
if (direction == UIPageViewControllerNavigationDirectionForward)
{
pageIndex++;
}
else
{
pageIndex--;
}
Upvotes: 0
Reputation: 546
- (void)viewDidLoad
{
[super viewDidLoad];
self.pvc = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pvc.view.frame = CGRectInset(self.view.bounds, 200, 200);
[self.view addSubview:self.pvc.view];
[self.pvc setViewControllers:@[[self randomVC]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
-(UIViewController*)randomVC
{
UIViewController *vc = [[UIViewController alloc] init];
UIColor *color = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
vc.view.backgroundColor = color;
return vc;
}
- (IBAction)previousButtonPressed:(id)sender {
[self.pvc setViewControllers:@[[self randomVC]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
}
- (IBAction)nextButtonPressed:(id)sender {
[self.pvc setViewControllers:@[[self randomVC]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
Try This It May Help you. I Have Made totally New Demo.
Upvotes: 2
Reputation: 2361
You need to call UIPageViewController
methods on your UIButton
tap. Here is what you need to do. Create a method for UIPageViewController
- (void)changePage:(UIPageViewControllerNavigationDirection)direction {
NSUInteger pageIndex = ((ViewControllerContainingUIImageView *) [_pageViewController.viewControllers objectAtIndex:0]).index;
if (direction == UIPageViewControllerNavigationDirectionForward)
{
pageIndex++;
}
else
{
pageIndex--;
}
initialViewController = [self viewControllerAtIndex:pageIndex];
if (initialViewController == nil) {
return;
}
[_pageViewController setViewControllers:@[initialViewController]
direction:direction
animated:YES
completion:nil];
}
Now call this method on UIButton
click providing direction forward/back
-(void)backSlide
{
[self changePage:UIPageViewControllerNavigationDirectionReverse];
}
-(void)forwardSlide
{
[self changePage:UIPageViewControllerNavigationDirectionForward];
}
Upvotes: 3