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

Posted

技术标签:

【中文标题】如何使用 UIButton 点击​​事件移动下一页和上一页?【英文标题】:How to move next and previous page using UIButton click event? 【发布时间】:2015-10-09 06:14:37 【问题描述】:

我正在使用以下代码来实现UIPageViewController

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

【问题讨论】:

【参考方案1】:

您需要在您的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 上调用此方法,点击提供前进/后退方向

-(void)backSlide

    [self changePage:UIPageViewControllerNavigationDirectionReverse];


-(void)forwardSlide

    [self changePage:UIPageViewControllerNavigationDirectionForward];

【讨论】:

【参考方案2】:
- (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];

试试这个它可以帮助你。我制作了全新的演示。

【讨论】:

感谢您的奉献和辛勤工作以获得所需的输出,我很感激。 :)【参考方案3】:

您可以只使用名为 a 的页面视图控制器的委托方法

- (void)changePage:(UIPageViewControllerNavigationDirection)direction

只需简单检查我在下面提到的条件。

 if (direction == UIPageViewControllerNavigationDirectionForward)
    
        pageIndex++;
    
    else
    
        pageIndex--;
    

【讨论】:

以上是关于如何使用 UIButton 点击​​事件移动下一页和上一页?的主要内容,如果未能解决你的问题,请参考以下文章

js怎么实现点击下一页的按钮播放音效

单击按钮移动到下一页时出错

确保在转到下一页之前发送了Google Analytics事件

如何有条件地转到pageViewController中的下一页?

如何解决我的提交按钮问题,我无法移动到下一页[重复]

js实现分页