我正在寻找一种可靠的方式来跟踪UIPageViewController当前索引。
这个问题众所周知;尽管正确显示了viewControllers,但很难跟踪当前索引。
我认为在SO社区中更新此主题会很好,因为它仍然由于某种原因尚未解决
我在这里浏览了很多帖子,但大多数答案都已过时或标记为不可靠(结果取决于用户是否进行了完全滑动或只是半滑等)
我已访问this帖子,但它没有提供任何明确正确的答案。
我试过了:
1)跟踪viewController的视图标记 - link - 始终返回0
2)在UIPageViewController方法中查看索引变量viewControllerBeforeViewController
和viewControllerAfterViewControlle
其结果无法预测,有时会跳过一个索引等。
有没有人想出一个好方法,可靠的方法来跟踪UIPageCiewController索引来使用它(例如打印当前索引)?
我很欣赏obj-c和swift的实现,但很快就是我正在寻找的。 p>
答案 0 :(得分:3)
与@ 0yeoj建议一样,委托设计模式就是解决方案。它需要在盒子外面思考一下。
让我们添加协议IndexDelegate并修改一下PageContentViewController
protocol IndexDelegate {
func showIndex(index:Int)
}
import UIKit
class PageContentViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
var delegate:IndexDelegate?
var pageIndex:Int = 0
var imageFile:String!
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.image = UIImage(named: self.imageFile)
}
override func viewDidAppear(animated: Bool) {
self.delegate!.showIndex(self.pageIndex)
}
现在在我们的父视图控制器中,我们符合协议
func showIndex(index: Int) {
print(index)
}
不要忘记设置yourPageContentViewControllerInstance.delegate = self
并继承协议YourParentViewController:UIViewController, UIPageViewControllerDataSourceDelegate, IndexDelegate {}
那就是它!工作完美可靠,并且完全没有延迟!
答案 1 :(得分:3)
这适用于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