我知道这可能是重复的,但我尝试了在线发现的其他解决方案,但似乎没有一个解决我的问题。
我有一个显示4个UIViewControllers的pageViewController。 首先滚动pageController滑动到第三个View控制器,但显示第二个ViewController。
这是我的代码:
_contentPageRestorationIDs = [[NSArray alloc]initWithObjects:@"PRPeripheralsOriantaionVC",@"PRSessionAnalysisDataVC", @"PRNavigationVC",@"PRVideoVC", nil];
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return self.contentPageRestorationIDs.count;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSString *vcRestorationID = viewController.restorationIdentifier;
NSUInteger index = [self.contentPageRestorationIDs indexOfObject:vcRestorationID];
NSLog(@"viewControllerBeforeViewController %@",vcRestorationID);
if (index == 0)
{
return nil;
}
index--;
return [self viewControllerAtIndex:index ];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSString *vcRestorationID = viewController.restorationIdentifier;
NSLog(@"viewControllerAfterViewController %@",vcRestorationID);
NSUInteger index = [self.contentPageRestorationIDs indexOfObject:vcRestorationID];
index ++;
if (index == self.contentPageRestorationIDs.count )
{
return nil;
}
return [self viewControllerAtIndex:index ];
}
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
{
NSLog(@"willTransitionToViewControllers %@ %lu",pendingViewControllers,(unsigned long)index);
}
-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
NSLog(@"didFinishAnimating %@",previousViewControllers);
}
答案 0 :(得分:1)
您有错误,请更新您的数据源方法:
h文件
@interface ViewController : UIPageViewController
@end
m档案
#import "ViewController.h"
@interface ViewController ()<UIPageViewControllerDataSource>
@property (strong, nonatomic) NSArray *pageColors;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.pageColors = @[@"green", @"yellow", @"black", @"red"];
self.dataSource = self;
UIViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = @[startingViewController];
[self setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Private method
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.pageColors count] == 0) || (index >= [self.pageColors count])) {
return nil;
}
NSString* pageColor = self.pageColors[index];
// Create a new view controller and pass suitable data.
UIViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:pageColor];
return pageContentViewController;
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController
{
NSString * identifier = viewController.restorationIdentifier;
NSUInteger index = [self.pageColors indexOfObject:identifier];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController
{
NSString * identifier = viewController.restorationIdentifier;
NSUInteger index = [self.pageColors indexOfObject:identifier];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageColors count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return [self.pageColors count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
@end
不要忘记为每个视图控制器更改Storyboard ID及其颜色([@“green”,@“yellow”,@“black”,@“red”])。 例如,对于读取视图控制器:
答案 1 :(得分:0)
正确的方法是为每个视图控制器定义pageIndex属性,然后在页面视图委托方法中设置它,使用索引方法的视图控制器,最终想要的代码应该是页面视图委托方法,将索引传递给索引路径的视图控制器并返回检索到的VC,就是这样。