gpt4 book ai didi

ios - 获取 UIPageViewController 当前索引的可靠方法

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:47 26 4
gpt4 key购买 nike

我正在寻找一种可靠的方式来跟踪 UIPageViewController 当前索引。

这个问题众所周知;尽管 viewControllers 被正确呈现,但很难跟踪当前索引。

我认为在 SO 社区刷新这个话题会很好,因为它由于某种原因仍未解决

我在这里浏览了很多主题,但大多数答案都已过时或标记为不可靠(结果取决于用户是完全滑动还是只滑动一半等)

我访问过this线程,但它没有提供任何明确的正确答案。

我试过:

1) 跟踪 viewController 的 View 标签 - link - 总是返回 0

2) 查看 UIPageViewController 方法 viewControllerBeforeViewControllerviewControllerAfterViewControlle 中的索引变量

它的结果是不可预测的,有时它会跳过一个索引等。

有没有人想出一个好的、可靠的方法来跟踪 UIPageCiewController 索引并使用它(例如打印当前索引)?

我很欣赏 obj-c 和 swift 实现,但 swift 是我正在寻找的。

最佳答案

这是针对 ObjC

ParentViewController

#import "PagesViewController.h"

// Delegate: PageViewDelegate
// Declared inside `PagesViewController`
//
@interface ParentViewController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate, PageViewDelegate>

@property (nonatomic) UIPageViewController *pageViewController;

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.dataSource = self;
self.pageViewController.view.frame = self.view.frame;

// im setting page 3 as the default page
//
[self.pageViewController setViewControllers:[NSArray arrayWithObject:[self viewControllerAtIndex:3]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [(PagesViewController *)viewController index];

if (index == 0) {
return nil;
}

index--;

return [self viewControllerAtIndex:index];
}

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

NSUInteger index = [(PagesViewController *)viewController index];

index++;

if (index == 5) {
return nil;
}

return [self viewControllerAtIndex:index];
}

- (PagesViewController *)viewControllerAtIndex:(NSInteger)index
{
PagesViewController *vc = [[PagesViewController alloc] init];

// set delegate here..
//
vc.delegate = self;

// other data
//
vc.index = index;
vc.titleLabel = [NSString stringWithFormat:@"Screen :%ld", (long)index];

return vc;
}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{ // The number of items reflected in the page indicator. return x; }

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{ // The selected item reflected in the page indicator. return x; }

// This is what you need
//
- (void)viewController:(id)VC didShowWithIndex:(long)index
{
NSLog(@"didShowWithIndex: %ld", index);
}

PagesViewController.h

@protocol PageViewDelegate <NSObject>

- (void)viewController:(id)VC didShowWithIndex:(long)index;

@end


@interface PagesViewController : UIViewController

@property (weak) id <PageViewDelegate> delegate;

@property (nonatomic) NSInteger index;

@property (nonatomic) NSString *titleLabel;

@end

PagesViewController.m

@implementation PagesViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.view.backgroundColor = [UIColor blackColor];

UILabel *titleLabel = [[UILabel alloc] initWithFrame:self.view.frame];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = self.titleLabel;
[self.view addSubview:titleLabel];
}

// Trigger delegate here
//
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

[self.delegate viewController:self didShowWithIndex:self.index];
}

@end

关于ios - 获取 UIPageViewController 当前索引的可靠方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36857455/

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