iOS – UIPageViewControllerDataSource方法调用两次

前端之家收集整理的这篇文章主要介绍了iOS – UIPageViewControllerDataSource方法调用两次前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我发现两个UIPageViewControllerDataSource方法存在一个奇怪的问题:第一次进入页面视图控制器场景并拖动控制器的内容时​​,无论拖动方向是什么,都会调用两个数据源方法.我想当我在第一页并向右拖动时,这意味着在第一页之前没有更多的页面,这两种方法都不应该被调用.如果我向左拖动,只应调用after方法.

我按照this post设置了视图控制器(除了我在故事板上没有单独的页面视图控制器.我使用传统的[UIPAgeViewController alloc] init]方法来实例化控制器).以下是我的代码

对于实际显示页面视图控制器内容的视图控制器(仅显示viewDidLoad的相关部分):

- (void)viewDidLoad 
{
    // initialize page view controller
    _pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;

    // set up the initial scene of the page view controller
    UIViewController *viewController = [self viewControllerAtIndex:0];
    [self.pageViewController setViewControllers:@[viewController]
                                      direction:UIPageViewControllerNavigationDirectionForward
                                       animated:NO
                                     completion:nil];

    // adjust the size of the page view controller,-44 to show the buttons at the bottom
    self.pageViewController.view.frame = CGRectMake(0,self.view.frame.size.width,self.view.frame.size.height - 44);

    // display the content of the page view controller
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
      viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = ((PWCPageContentViewController *)viewController).index;
    if (index == 0 || index == NSNotFound) {
        return nil;
    }
    --index;
    return [self viewControllerAtIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
       viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = ((PWCPageContentViewController *)viewController).index;
    if (index == self.numberOfPages - 1 || index == NSNotFound) {
        return nil;
    }
    ++index;
    return [self viewControllerAtIndex:index];
}

- (PWCPageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
    PWCPageContentViewController *viewController =
    [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
    viewController.index = index;
    NSString *text = [self.notes getNoteAtIndex:index];
    [viewController.textView setText:text];

    return viewController;
}

对于PWCPageContentViewController.h:

#import <UIKit/UIKit.h>

@class PWCNotes;

@interface PWCPageContentViewController : UIViewController <UITextViewDelegate>

@property NSUInteger index;
@property (weak,nonatomic) IBOutlet UITextView *textView;

- (void)dismissKeyboard;

@end

对于PWCPageContentViewController.m:

#import "PWCPageContentViewController.h"

@interface PWCPageContentViewController ()

@end

@implementation PWCPageContentViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.textView.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dismissKeyboard
{
    if ([self.textView isFirstResponder]) {
        // dismiss the keyboard when hitting save
        [self.textView resignFirstResponder];
    }
}

@end

这是一个已知的问题还是我错过了什么?

解决方法

UIPageViewController使用这些波纹管方法获取后面和前面的ViewController.
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;

虽然它很奇怪但是它是一个已知的问题.每次调用Before和After方法获取VC.如果没有下一个VC则返回nil,如果没有先前的VC,则datasourceDelegate返回nil,否则返回VC的索引.

在UIPageViewControllerDelegate中,有一个名为的函数

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers;

看看这可能有助于从pendingViewControllers数组中获取当前或下一个/上一个viewcontroller.

希望这可以帮助.. :)

原文链接:https://www.f2er.com/iOS/334138.html

猜你在找的iOS相关文章