使用UIPageViewController进行导航

时间:2014-04-21 12:48:44

标签: ios navigation uipageviewcontroller uipagecontrol

对不起我的英文:))

我使用UIPageViewController在页面之间导航。第一页是所有页面的内容表。当我点击内容表中的元素时,我尝试使用上面的方法更改页面:

-(void) flipToPage:(int)index {
BaseArtViewController *pageViewController = [self viewControllerAtIndex:index];
NSArray *viewControllers = [NSArray arrayWithObjects:pageViewController, nil];

if (currentIndex < index){
    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
}
else if (currentIndex > index){
    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:NULL];
}

// For change pageControl aspect
NSArray *subviews = self.pageController.view.subviews;
UIPageControl *thisControl = nil;
for (int i=0; i<[subviews count]; i++) {
    if ([[subviews objectAtIndex:i] isKindOfClass:[UIPageControl class]]) {
        thisControl = (UIPageControl *)[subviews objectAtIndex:i];
        thisControl.currentPage = index;
    }
}

}

这是有效但不正确的,内容表和页面访问之间没有过渡。

当我到达点击的页面时,如果我尝试访问上一页,例如我在第4页,我想继续第3页,但我在第0页(内容表)返回。你知道一个解决方案,或者有人可以解释我该怎么做???

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

谢谢,

这是我的代码:

-(UIViewController*) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = [(BaseArtViewController *)viewController index];
    NSLog(@"Index Before : %i", index);

    if (index == 0) return nil;

    // Change index
    index--;

    // Stock current index
    currentIndex = index;

    return [self viewControllerAtIndex:index];
}


-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = [(BaseArtViewController *)viewController index];
    NSLog(@"Index After : %i", index);

    // Change index
    index++;

    if (index == nbPages) return nil;

    // Stock current index
    currentIndex = index;

    return [self viewControllerAtIndex:index];
}

初始化:

-(void) initPresentation {
    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    [[self.pageController view] setFrame:[[self view] bounds]];

    BaseArtViewController *initialViewController = [self viewControllerAtIndex:currentIndex];

    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

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

    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];
}

基类:BaseArtViewController.h     #import

@interface BaseArtViewController : UIViewController

@property (assign, nonatomic) NSInteger index;
@property (assign, nonatomic) NSArray *tabS;

@end

基类:BaseArtViewController.m

#import "BaseArtViewController.h"

@interface BaseArtViewController ()
@end

@implementation BaseArtViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSLog(@"Index Page : %i", self.index);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end