gpt4 book ai didi

ios - UIPageViewControllerDataSource 第一次运行时调用 viewControllerAfterViewController 两次

转载 作者:行者123 更新时间:2023-11-29 02:01:46 25 4
gpt4 key购买 nike

我知道这可能是重复的,但我尝试了在网上找到的其他解决方案,但似乎都没有解决我的问题。

我有一个 pageViewController,它显示 4 个 UIViewController。首先滚动 pageController 滑动到第三个 View Controller ,但显示第二个 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);
}

最佳答案

你有一个错误,更新你的数据源方法:

h 文件

@interface ViewController : UIPageViewController

@end

文件

#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

enter image description here

不要忘记为每个 View Controller 更改 Storyboard ID 及其颜色([@"green", @"yellow", @"black", @"red"])。例如,对于读取 View Controller :

enter image description here

You can find here very useful tutorial.

关于ios - UIPageViewControllerDataSource 第一次运行时调用 viewControllerAfterViewController 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30284975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com