iOS:UIPageViewController - 使用按钮跳转到下一页

时间:2014-05-23 18:07:24

标签: ios uibutton uipageviewcontroller

我在PageViewController中有一系列VC,用户可以用手指从左到右导航。我需要添加按钮,这些按钮基本上执行与手指滑动相同的操作,通过VC向左或向右移动一个按钮。我怎样才能做到这一点?现在我正在使用这两种方法动态设置VC作为用户滑动:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController          viewControllerBeforeViewController:(UIViewController *)viewController;

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;

如果用户点击按钮,我可以做同样的事情吗?

2 个答案:

答案 0 :(得分:11)

您可以使用页面视图控制器上的setViewControllers:direction:animated:completion:以编程方式使用过渡动画设置当前显示的视图控制器。

这是一个显示具有随机背景颜色的视图控制器的示例。您可以调整此项以使用特定的视图控制器。

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

答案 1 :(得分:0)

如果可以通过添加按钮导航页面(上一页,下一页)&也可以通过滑动打开页面控件。 请仔细阅读以下内容:

//Set Delegate & Data Source for PageView controller [Say in View Did Load]
   self.pageViewController.dataSource = self;
   self.pageViewController.delegate = self;



// PageBefore & After When User Scroll to move next or previous page 

 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{

    [_nextBtn setTitle:@"Next" forState:UIControlStateNormal];

    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

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

    if (index > 0)
    {
        index--;
    }
    else
    {
        return nil;
    }

    return [self viewControllerAtIndex:index];

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

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


if (index < 3)
{
    index++;
}else
{
    return nil;
}
return [self viewControllerAtIndex:index];}

//To Match Exact Index page view when scrolled & navigated using button action. Place button index when page is been translated.



 - (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers{    
buttonIndex = (int)((PageContentViewController*) pendingViewControllers.firstObject).pageIndex;}

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{    
if (buttonIndex == 0)
{
    _backButton.hidden = true;
}else if (buttonIndex == [self.pageImages count] - 1)
{
    _backButton.hidden = false;
    [_nextBtn setTitle:@"Begin" forState:UIControlStateNormal];
}else
{
    _backButton.hidden = false;
}
}

//上一个,下一个按钮操作

-(void)backBtnClicked:(id)sender{
if (buttonIndex > 0)
{
buttonIndex -= 1;
}
    if (buttonIndex < 1) {
        _backButton.hidden = YES;
    }
    if (buttonIndex >=0) {
         [_nextBtn setTitle:@"Next" forState:UIControlStateNormal];
        PageContentViewController *startingViewController = [self viewControllerAtIndex:buttonIndex];
        NSArray *viewControllers = @[startingViewController];
        [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    }}

-(void)nextBtnAction:(id)sender{
if (buttonIndex < 3)
{
buttonIndex += 1;
}

if(buttonIndex == _pageImages.count){
   //Navigate Outside Pageview controller        
}   else{        
    if (buttonIndex ==3) {

        [_nextBtn setTitle:@"Begin" forState:UIControlStateNormal];
    }
    _backButton.hidden = NO;

    PageContentViewController *startingViewController = [self viewControllerAtIndex:buttonIndex];
    NSArray *viewControllers = @[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}}

//BUTTON INDEX
-(NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{
    return [self.pageImages count];}

-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{
    return  buttonIndex;}