如何在两个UIView控制器之间跳转

时间:2015-08-01 13:32:51

标签: ios objective-c uiviewcontroller uiscrollview

我想跳过两个UIViewcontrollers

通过滑动操作。

目前,我实施了UIScrollView,并在其上添加了UIViewcontrollers(vc1和vc2),所以

on vc1

  

向左滑动:跳转到vc2

     

向右滑动:什么都不做

on vc2

  

向左滑动:什么都不做

     

向右滑动:跳转到vc1

但我想要的是:

on vc1

  

向左滑动:跳转到vc2

     

向右滑动:跳转到vc2

on vc2

  

向左滑动:跳转到vc1

     

向右滑动:跳转到vc1

那么,如何实现?

1 个答案:

答案 0 :(得分:0)

试试这个

步骤:

  1. 在视图控制器中创建UIPageViewController
  2. self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    1. UIPageViewController
    2. 上添加您的两个视图控制器

      NSArray *viewControllers = //add your two view controllers in array;

      [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

      1. 设置数据源并将UIPageViewController添加为子视图控制器
      2. self.pageController.dataSource = self; [self addChildViewController:self.pageController];

        现在,您需要实现两个方法

        - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
        
            NSUInteger index = [(ViewController *)viewController index];
        
            if (index == 0) {
                return nil;
            }
        
            // Decrease the index by 1 to return
            index--;
        
            return viewControllers[index];//[self viewControllerAtIndex:index];//viewControllerAtIndex
        
        }
        
        - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
        
            NSUInteger index = [(ViewController *)viewController index];
        
            if (index == 1) {
                return nil;
            }
            index++;
        
            return viewControllers[index];
        
        }