我有一个UIPageViewController,我用它来处理我制作的自定义日历。日历是日视图,因此基本上,每天都是相同类型的视图控制器,显示当天的时间以及用户具有的任何事件。
当我向前或向后滑动页面视图控制器以更改日期时,它似乎在大约两次滑动中失序。
当调用每个视图控制器时,它会传递一个日期,一旦视图控制器加载,我就会将日期字符串分配给导航栏,并使用此日期从字典中获取事件。
大多数情况下,导航栏文字是正确的(在滑动时它似乎不会跳过任何日子),但即使导航文本正确,它有时也会显示错误日期的事件。为了测试这个,我创建了一个标签来打印Dictionary Key字符串(我用它来获取特定日期的事件),然后将该键打印到标签上以查看它是否与导航栏标题匹配。
奇怪的是,标签和导航栏都是从同一天分配的,但有时会显示不同的字符串。通常情况下,日期在标签中仅偏移一个,而导航是正确的。
这是我的ViewController实例化UIPageViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadDatesArray];
self.navigationController.title = @"Day View";
// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CalendarDayPageViewController"];
self.pageViewController.dataSource = self;
self.pageViewController.delegate = self;
self.pageViewController.startDateForDayView = _startDateForDayView;
self.pageViewController.hashMap = _hashMap;
EICalendarDayContentViewController *startingVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CalendarDayPageContentController"];
[self.pageViewController setViewControllers:@[startingVC] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
NSArray *subviews = self.pageViewController.view.subviews;
UIPageControl *thisControl = nil;
for (int i=0; i<[subviews count]; i++) {
if ([[subviews objectAtIndex:i] isKindOfClass:[UIPageControl class]]) {
thisControl = (UIPageControl *)[subviews objectAtIndex:i]; } }
thisControl.hidden = true;
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 49);
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
_index = 250;
_stepsBack = 0;
_stepsForward = 0;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
_index--;
return [self viewControllerAtIndex:_index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
_index++;
return [self viewControllerAtIndex:_index];
}
- (EICalendarDayContentViewController *)viewControllerAtIndex:(NSInteger)index
{
NSDate *date = [_DateArray objectAtIndex:index];
EICalendarDayContentViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"CalendarDayPageContentController"];
vc.dateForView = date;
vc.pageIndex = index;
return vc;
}
- (void)loadDatesArray {
_DateArray = [[NSMutableArray alloc] init];
for (int i = -250; i < 250; i++) {
NSDate *newDate = [_startDateForDayView dateByAddingTimeInterval:60*60*24*i];
NSLog(@"Date Array = %@ atIndex = %d", newDate, i);
[_DateArray addObject:newDate];
}
}
[self loadDatesArray]加载一个包含500个日期的数组;在当前日期之前250和之后250。
因此,_index从250(当前日)开始,beforeViewController调用date 249,afterViewController调用251等...
请帮忙..长期坚持这一点。