如何使用 UIButton 单击事件移动下一页和上一页?

How to move next and previous page using UIButton click event?

我正在使用以下代码来实现 UIPageViewController。如何使用 UIButton 点击​​事件移动下一页和上一页?

-(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];
}

您需要在 UIButton 水龙头上调用 UIPageViewController 方法。这是您需要做的。为 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];
}

现在在 UIButton 单击提供方向时调用此方法 forward/back

-(void)backSlide
{
    [self changePage:UIPageViewControllerNavigationDirectionReverse];
}

-(void)forwardSlide
{
    [self changePage:UIPageViewControllerNavigationDirectionForward];
}

您可以只使用名为 a

的页面视图控制器的委托方法
- (void)changePage:(UIPageViewControllerNavigationDirection)direction

然后简单地检查我在下面提到的条件。

 if (direction == UIPageViewControllerNavigationDirectionForward)
    {
        pageIndex++;
    }
    else
    {
        pageIndex--;
    }
- (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];
}

试试这个可能对你有帮助。我做了全新的Demo。