过去几天我一直在努力解决这个问题,经过这个杂耍之后,我发现我只需要数据源方法的当前索引来更新当前可见的页码
我有这个UIPageViewController
数据源方法,我需要使用当前索引来获取委托方法previousViewControllers transitionCompleted:(BOOL)completed
的当前可见页面
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController {
NSString *path = [[NSBundle mainBundle] pathForResource:@"pages" ofType:@"pdf"];
NSURL *pdfurl = [NSURL fileURLWithPath:path];
PDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfurl);
contentViewController = [[ContentViewController alloc] initWithPDFAtPath:path];
currentIndex = [modelArray indexOfObject:[(ContentViewController *)viewController page]];
if (currentIndex == totalPages - 1) {
return nil;
}
contentViewController.page = [modelArray objectAtIndex:currentIndex + 1];
return contentViewController;
}
我对如何将当前索引语句从datasource方法写入委托方法以更新当前可见页面感到困惑。
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
if (!completed)
{
return;
}
//currentIndex = [[self.pageViewController.viewController lastObject]];
currentIndex = [self indexOfObject:contentViewController];
[self displaycurrentIndex:currentIndex];
//self.pageControl.currentPage = currentIndex;
}
我该如何纠正?
答案 0 :(得分:26)
看起来您应该能够通过以下方式获取当前索引:
ContentViewController *viewController = [self.pageViewController.viewControllers lastObject];
currentIndex = [modelArray indexOfObject:[viewController page]];
答案 1 :(得分:19)
对于swift:
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool){
let pageContentViewController = pageViewController.viewControllers![0] as! ViewController
let index = pageContentViewController.pageIndex
}
答案 2 :(得分:8)
如果你实现ModelController(如Apple示例中所示),请使用它的方法 indexOfViewController
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
myDataViewController *currentView = [pageViewController.viewControllers objectAtIndex:0];
NSInteger currentIndex = [_modelController indexOfViewController:currentView];
[self displayPageNumber:currentIndex];
}
答案 3 :(得分:4)
实施委托方法
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
NSLog(@"Current Page = %@", pageViewController.viewControllers);
UIViewController *currentView = [pageViewController.viewControllers objectAtIndex:0];
if ([currentView isKindOfClass:[FirstPageViewController class]]) {
NSLog(@"First View");
}
else if([currentView isKindOfClass:[SecondPageViewController class]]){
NSLog(@"Second View");
}
else if([currentView isKindOfClass:[ThirdViewController class]]){
NSLog(@"Third View");
}
}
// pageViewController.viewControllers总是返回当前可见的View ViewController
答案 4 :(得分:4)
我在pageViewController上面临同样的问题,每次我刷我的pageViewController它调用我的委托两次,我无法找到问题。我的数组是动态的,所以我想从pageController得到正确的索引值。首先设置pageViewController的委托。像这样: -
self.pageViewController.delegate = self;
然后将此委托方法添加到您的班级
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
if (completed) {
NSInteger currentIndex = ((PageContentViewController *)self.pageViewController.viewControllers.firstObject).pageIndex;
NSLog(@"%ld",(long)currentIndex);
}
}
PageContentViewController是您显示pageViewController数据的类。
运行您的项目,如果一切正常,您将获得索引值。 PageIndex 是获取当前索引的Integer值。希望它会对你有所帮助。
由于
Mandeep Singh
答案 5 :(得分:2)
要获取当前索引,您必须执行以下操作:
-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
NSUInteger index = [self.viewControllers indexOfObject: [pageViewController.viewControllers lastObject]];
NSLog(@"INDEX: %lu",(unsigned long)index);
}
答案 6 :(得分:2)
我也在努力解决这个问题,其他解决方案都没有为我工作,但终于能够使用委托方法解决它了:
在PageContentViewController.h中第一次 添加以下属性:
@property NSUInteger pageIndex;
第二次在PageContentViewController(而不是PageViewController)中创建委托协议。
在PageContentViewController.h中:
@protocol PageContentDelegate <NSObject>
- (void)sendBackPageIndex:(NSUInteger)index;
@end
@interface LKFPageContentViewController : (UIViewController)
@property (weak, nonatomic) id<LKFPageContentDelegate> delegate;
....
@end
PageContentViewController.m中的第三在viewDidAppear中执行委托方法
//Note that this must be done in viewDidAppear not viewDidLoad
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
[self.delegate sendBackPageIndex:self.pageIndex];
第四,在PageViewController.m中实现委托方法
#pragma mark - PageContentView delegate methods
- (void)sendBackPageIndex:(NSUInteger)index {
//This is where you get your current page index
NSUInteger currentPageIndex = index;
}
第五,在PageViewController.m中确保传递pageIndex值并设置委托
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index {
if (([self.arrayOfTitles count] == 0) || (index >= [self.arrayOfTitles count])) {
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
pageContentViewController.pageIndex = index;
pageContentViewController.delegate = self;
return pageContentViewController
}
答案 7 :(得分:2)
我已经解决了这个问题:
*父视图控制器(PageViewController)*
@interface MyParentPageViewController () <UIPageViewControllerDataSource>
@property (nonatomic, assign) NSUInteger currentPageIndex;
@end
@implementation MyParentPageViewController
- (void)viewDidLoad
{
[super viewDidLoad];
MyChildViewController *rootChildViewController = [MyChildViewController controllerWithPageIndex:self.currentPageIndex];
[self setViewControllers:@[rootChildViewController]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES completion:nil];
self.dataSource = self;
}
#pragma mark - Page View Controller DataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(MyChildViewController *)viewController
{
if (viewController.pageIndex == self.sourceArray.count - 1) {
return nil;
}
MyChildViewController *vc = [MyChildViewController controllerWithPageIndex:viewController.pageIndex + 1];
return vc;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(CHHomeDetailTableController *)viewController
{
if (viewController.pageIndex == 0) {
return nil;
}
MyChilViewController *vc = [MyChildViewController controllerWithPageIndex:viewController.pageIndex - 1];
return vc;
}
@end
*子视图控制器*
@implementation MyChildViewController
- (void)viewWillAppear
{
MyParentPageViewController *parentPageViewController = (MyParentPageViewController *)self.parentViewController;
parentViewController.currentPageIndex = self.pageIndex;
}
@end
例如,当您向右滑动时,parentPageViewController的currentPageIndex为10或其他一些值,parentViewController.currentPageIndex为9,如果为0,则没有任何反应;如果你向左滑动,parentPageViewController.currentPageIndex将是11,如果显示controller.view是最后一个,也没有发生任何事情。
答案 8 :(得分:1)
你可以试试这个。这将为您提供当前页面。
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
UIViewController * viewController = [previousViewControllers lastObject];
NSUInteger previousPage = [(PageContentViewController *)viewController index];
NSUInteger currentPage = previousPage+1;
}
答案 9 :(得分:0)
这是我如何确定当前页面索引,用户是向左滑动还是向右滑动以及是否在滑动过程中更改滑动方向手势。只需确保视图控制器中有pageIndex属性即可存储索引:
func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
let currentPageIndex = (pendingViewControllers.last! as! ViewController).pageIndex!
}
答案 10 :(得分:0)
没有很好的方法可以做到这一点,但我认为最可靠的方法是观察UIPageViewControllerDelegate
次来电:
var index: Int
var proposedViewController: UIViewController?
// MARK: UIPageViewControllerDelegate
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
guard let proposedViewController = proposedViewController,
let index = myViewControllers?.index(of: proposedViewController) else {
return
}
self.index = index
}
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
guard pendingViewControllers.count == 1,
let proposedViewController = pendingViewControllers.first else {
return
}
self.proposedViewController = proposedViewController
}
答案 11 :(得分:0)
我的方法很简单:
实例化页面(控制器)后,我传入页面索引,这是众所周知的。
然后在下面的委托调用中,如果转换完成,则在获取并使用关联的页面索引更新currentPage之前,我将获得当前显示的控制器并将其转换为自定义页面类。
为简单起见,我将数据源设置为WalkThroughController
并嵌入了页面视图控制器。
extension WalkThroughController: UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
guard finished, let currentPageController = pageViewController.viewControllers?.last as? WalkThroughPageViewController else {
return
}
self.currentPage = currentPageController.pageIndex
}
}
要点是设置在数据源中创建页面时的页面索引。通过这种方法,很容易在以后检索它。