我有两个标识符为“one”和“two”的故事板,我有一个UIPageViewController及其子类。在子类中,我有以下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.dataSource = self;
self.delegate = self;
counter = 0;
[self setViewControllers:@[[self.storyboard instantiateViewControllerWithIdentifier:@"one"]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[ViewController class]]){
return [self.storyboard instantiateViewControllerWithIdentifier:@"one"];
}
return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[secondViewController class]]){
return [self.storyboard instantiateViewControllerWithIdentifier:@"two"];
}
return nil;
}
此代码运行良好,但有问题!卷轴是无限的;在我的情况下,我需要脱掉无限卷轴。我怎么能这样做?
首先是创建一个计数器并在我的方法中进行检查:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
if (counter < 1) {
NSLog(@"Don't get back");
return nil;
}
......
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
if (counter >= 1) {
NSLog(@"Don't go anymore");
return nil;
}
....
}
第二件事是尝试使用 counter ++ 和 counter - 。这对我来说是个大难题,任何人都有更好的想法吗?
答案 0 :(得分:1)
如果您只有两个页面,那么只需检查您何时收到页面请求,如果当前页面不是您要提供的页面。为此,您需要一种方法来识别它。
如果每个页面都有不同的类,您只需检查该类。否则你需要一些其他标准。
如果使用自定义类:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[ClassOfSecondViewController class]])
{
return [self.storyboard instantiateViewControllerWithIdentifier:@"one"];
}
return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[ClassOfFirstViewController class]])
{
return [self.storyboard instantiateViewControllerWithIdentifier:@"two"];
}
return nil;
}
答案 1 :(得分:0)
情况稍有变化。
- (nullable UIViewController *)pageViewController:(nonnull UIPageViewController *)pageViewController viewControllerBeforeViewController:(nonnull UIViewController *)viewController {
NSUInteger currentIndex = [pages indexOfObject:viewController];
NSUInteger previousIndex = ((currentIndex - 1) % pages.count);
if (viewController == yourFirstViewController)
return nil;
return pages[previousIndex];
}
- (nullable UIViewController *)pageViewController:(nonnull UIPageViewController *)pageViewController viewControllerAfterViewController:(nonnull UIViewController *)viewController {
NSUInteger currentIndex = [pages indexOfObject:viewController];
NSUInteger nextIndex = ((currentIndex + 1) % pages.count);
if (viewController == yourLastViewController)
return nil;
return pages[nextIndex];
}