UIPageViewController的DataSource委托连续两次调用一次

时间:2014-07-28 15:00:25

标签: ios ipad

这可能看起来像是之前提出的问题(PageViewController delegate functions called twice),但问题是我无法将该解决方案应用于我的问题。

您可能会注意到我正在开发日历应用程序并使用UIPageViewController来管理我的年度日历显示。正如你所看到我使用UIPageViewControllerTransitionStylePageCurl并且当用户卷曲页面(向前/向后)时,它们各自的委托被调用两次,这给了我2年的增量或减量,基于执行的委托。现在我需要找出导致此问题的原因并阻止它执行两次。

我知道在给出我的下一页或上一页的那些委托中返回一个viewController是很重要的,因此我只是刷新了viewController的视图,以便我可以用新数据渲染视图。我还尝试了另一个名为willTransitionToViewControllers的委托,但不会让我到处都是因为willTransitionToViewControllers只会在执行之后执行 viewControllerAfterViewController和viewControllerBeforeViewController。

有人帮助我理解并解决了这个问题。

 - (void)addCalendarViews
{
    CGRect frame = CGRectMake(0., 0., self.view.frame.size.width, self.view.frame.size.height);

    pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
                                                           navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                                                    options:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:UIPageViewControllerSpineLocationNone] forKey:UIPageViewControllerOptionSpineLocationKey]];
    pageViewController.doubleSided = YES;
    pageViewController.delegate = self;
    pageViewController.dataSource = self;

    YearCalendarViewController *yearController = [[YearCalendarViewController alloc] init];


    NSArray *viewControllers = [NSArray arrayWithObject:yearController];

    [self.pageViewController setViewControllers:viewControllers
                                  direction:UIPageViewControllerNavigationDirectionReverse
                                   animated:YES
                                 completion:nil];

    [self addChildViewController:pageViewController];
    [self.view addSubview:pageViewController.view];

    [pageViewController didMoveToParentViewController:self];

     CGRect pageViewRect = yearController.view.bounds;
     self.pageViewController.view.frame = pageViewRect;


     viewCalendarMonth = [[SGMonthCalendarView alloc] initWithFrame:frame];
     [self.view addSubview:viewCalendarMonth];

     arrayCalendars = @[pageViewController.view, viewCalendarMonth];
}




 -(UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
 {
     NSDate *currentDate = [[SGSharedDate sharedManager] currentDate]; 
     NSDateComponents *currentDateComp = [NSDate returnDateComponentsForDate:currentDate];
     self.nextDate = [NSDate dateWithYear:currentDateComp.year+1 month:currentDateComp.month day:currentDateComp.day];
    [[SGSharedDate sharedManager] setCurrentDate:nextDate];


   {
      //I m reloading viewController's view here to display the new set of data for the increamented date.
   }
    NSLog(@"After Dates====>%@", nextDate);
    return viewController;
 }



 -(UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
 {
     NSDate *currentDate = [[SGSharedDate sharedManager] currentDate]; 
     NSDateComponents *currentDateComp = [NSDate returnDateComponentsForDate:currentDate];
     nextDate = [NSDate dateWithYear:currentDateComp.year-1 month:currentDateComp.month day:currentDateComp.day];
     [[SGSharedDate sharedManager] setCurrentDate:nextDate];


     {
      //I m reloading viewController's view here to display the new set of data for the decremented date.
   }
     NSLog(@"Before Dates====>%@", nextDate);
     return viewController;
}

1 个答案:

答案 0 :(得分:5)

UIPageController往往会向前缓存(并返回),因此当您从视图控制器N向前滑动时,N+1会加载但是N+2也会默默地加载,因此在事件中你刷两次你的内容已经加载。

但是由于你在委托方法中有一个看不见的副作用(递增日期),你会受到这种影响。

我的建议是从你的委托中删除约会副作用,并以某种方式将日期代码绑定到呈现的视图控制器(委托或属性),然后捕获pageController的didFinishAnimating方法。

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {

    if (completed) {

        if ([[self.pageViewController.viewControllers firstObject] isKindOfClass:[MyCalenderViewController class]]) {

            currentDate = [self workOutCurrentDateForVC:[self.pageViewController.viewControllers firstObject]];

        }

    }

}

副作用是代码气味。尽量避免。

修改

我刚注意到您正在回收视图控制器并使用新的日期点重新加载它。不要这样做。您需要将视图控制器视为图书的单个页面。因此,生成一个新的并将其设置为月...

简单地举例......

@interface YearViewController : UIViewController 

@property NSDate *focusedYear;

-(instancetype)initWithYear:(NSDate)adate;

@end

@implementation YearViewController

-(instancetype)initWithYear:(NSDate)adate {
     self = [super initWithNibName:nil bundle:nil];
     if(self) {
         self.focusedYear = adate;
         ....
     } 
}

@end

因此,在页面控制器委托中,您只需根据前一个日期(以及您正在分页的方向)提供新的“页面”

-(UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
 {
     YearViewController *yvc = (YearViewController *)viewController;

     NSDate *currentDate = yvc.focusedYear; 
     NSDateComponents *currentDateComp = [NSDate returnDateComponentsForDate:currentDate];
     NSDate *nextDate = [NSDate dateWithYear:currentDateComp.year+1 month:currentDateComp.month day:currentDateComp.day];

     YearViewController *freshYVC = [[YearViewController alloc] initWithDate:nextDate];

    return freshYVC;
 }

关于副作用的评论仍然存在。