问题描述
我正在寻找一种可靠的方式来跟踪UIPageViewController当前索引.
I'm looking for a reliable way to keep track of UIPageViewController current index.
这个问题是众所周知的;尽管viewControllers可以正确显示,但是很难跟踪当前索引.
The problem is well known; Although viewControllers are being presented properly, it hard to keep track of the current index.
我认为最好在整个SO社区中刷新此主题,因为由于某些原因该主题仍未解决
I thought it will be good to refresh this topic across SO community since it remains unsolved for some reason
我在这里浏览了很多主题,但是大多数答案已经过时或标记为不可靠(结果取决于用户是完全滑动还是只滑动了一半等等)
I've browsed through many threads here, but most answers are outdated or marked as unreliable(The result depends on if user did a full swipe or just half swiped etc)
我已经访问过此线程,但是它没有提供任何明确正确的答案.
I've visited this thread, but it doesn't provide any explicitly correct answer.
我尝试过:
1)跟踪viewController的视图标签-链接-始终返回0
1) Keeping track of viewController's view tag - link - always returns 0
2)在两个UIPageViewController方法viewControllerBeforeViewController
和viewControllerAfterViewControlle
2) Looking at index variable in both UIPageViewController methods, viewControllerBeforeViewController
and viewControllerAfterViewControlle
其结果不可预测,有时会跳过一个索引,等等.
its results is unpredictable, sometimes it skips over one index etc.
是否有人想出一种很好的方法来跟踪UIPageCiewController索引以利用它(例如打印当前索引)?
我很欣赏obj-c和swift的实现,但是swift是我想要的.
I'd appreciate both obj-c and swift implementation, but swift is the one I'm looking for.
推荐答案
这是针对ObjC
#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);
}
@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
@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
这篇关于一种获取UIPageViewController当前索引的可靠方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!