使用三个ViewController

时间:2016-01-11 07:36:23

标签: ios objective-c uipageviewcontroller

我正在关注此链接CustomPageControlForPageViewController以实现SwipeView菜单。在此项目中,有only two ViewControllers涉及滑动过渡。但我的要求是three ViewControllers喜欢。所以我不知道如何使用三个ViewControllers实现UIPageViewController delegate methods,它们将处理从一个视图到另一个视图的转换。以下是两个ViewControllers给定链接的代码。

swipe.h

@interface RootViewController : UIViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>

@property (strong, nonatomic) UIPageViewController *pageViewController;
@property (strong, nonatomic) UIView *pageControlCustomView;

@end

swipe.m

@interface RootViewController () {

NSArray *viewControllers;
UIView *selectionBar;
int SELECTOR_WIDTH;
int SELECTOR_Y;
UIButton *leftButton;
UIButton *rightButton;

moveSelectorBarLeftBlockType moveSelectorBarLeft;
moveSelectorBarRightBlockType moveSelectorBarRight;

UIViewController *firstVC;
UIViewController *secondVC;
}

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];
SELECTOR_WIDTH = self.view.frame.size.width/2;
SELECTOR_Y = self.navigationController.navigationBar.frame.size.height + Y_OFFSET_BELOW_NAVBAR + HEIGHT;

UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor blackColor];

// Create page view controller
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.dataSource = self;
self.pageViewController.delegate = self;

firstVC = [self.storyboard instantiateViewControllerWithIdentifier:@"firstViewController"];
secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];

viewControllers = @[firstVC];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

}

#pragma mark - Page View Controller Data Source

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
if (self.pageViewController.viewControllers[0] == secondVC)
    return firstVC;
return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
if (self.pageViewController.viewControllers[0] == firstVC)
    return secondVC;
return nil;
}


- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
if (completed) {
    if (previousViewControllers[0] == secondVC) {
        moveSelectorBarLeft();
    }
    else {
        moveSelectorBarRight();
    }
}
}

1 个答案:

答案 0 :(得分:0)

我建议您使用自己的数组来存储viewControllers的顺序。

以下是我使用此委托的方式

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

    NSUInteger index = [self.viewControllers indexOfObject:viewController];
    if (index == 0) { return nil;}
    index--;
    UIViewController *targetViewController = [self.viewControllers objectAtIndex:index];

    return targetViewController;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = [self.viewControllers indexOfObject:viewController];
    index++;
    if (index >= self.viewControllers.count) { return nil;}

    UIViewController *targetViewController = [self.viewControllers objectAtIndex:index];

    return targetViewController;
}