我正在使用UIPageViewController和UIPageControl。由于某种原因,索引上的0-> 1转换持续被调用两次。例如,使用以下代码:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
self.index --;
NSLog(@")self.index --;");
if(self.index<1){
self.index = 1;
}
return [self viewControllerAtIndex: self.index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSLog(@")self.index ++;");
self.index ++;
if(self.index==5){
[self dismissViewControllerAnimated:NO completion:nil];
return nil;
}
return [self viewControllerAtIndex: self.index];
}
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index{
self.index = index;
NSLog(@"index is %d", self.index);
UIViewController *next1 = [[UIViewController alloc] init];
next1.view.backgroundColor = BACKGROUND_COLOR;
next1.view.userInteractionEnabled = NO;
return next1;
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return 5;
}
-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return self.index;
}
我得到了这个输出:
2014-12-22 12:10:04.349 BuzzJudge[665:142581] index is 0
2014-12-22 12:10:05.030 BuzzJudge[665:142581] )self.index ++;
2014-12-22 12:10:05.031 BuzzJudge[665:142581] index is 1
2014-12-22 12:10:05.032 BuzzJudge[665:142581] )self.index --;
2014-12-22 12:10:05.032 BuzzJudge[665:142581] index is 1
2014-12-22 12:10:05.444 BuzzJudge[665:142581] )self.index ++;
2014-12-22 12:10:05.445 BuzzJudge[665:142581] index is 2
2014-12-22 12:10:05.844 BuzzJudge[665:142581] )self.index ++;
2014-12-22 12:10:05.845 BuzzJudge[665:142581] index is 3
2014-12-22 12:10:06.263 BuzzJudge[665:142581] )self.index ++;
2014-12-22 12:10:06.264 BuzzJudge[665:142581] index is 4
2014-12-22 12:10:06.627 BuzzJudge[665:142581] )self.index ++;
如果我略微修改代码以允许0索引(我最初在做什么)它变得更糟,在0和1索引之间反弹而不是仅仅0-> 1,如下所示:
2014-12-22 12:13:46.404 BuzzJudge[672:144489] index is 0
2014-12-22 12:13:48.147 BuzzJudge[672:144489] )self.index ++;
2014-12-22 12:13:48.148 BuzzJudge[672:144489] index is 1
2014-12-22 12:13:48.149 BuzzJudge[672:144489] )self.index --;
2014-12-22 12:13:48.150 BuzzJudge[672:144489] index is 0
2014-12-22 12:13:48.561 BuzzJudge[672:144489] )self.index ++;
2014-12-22 12:13:48.562 BuzzJudge[672:144489] index is 1
使用此修改后的代码:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
self.index --;
NSLog(@")self.index --;");
if(self.index<0){
self.index = 0;
}
return [self viewControllerAtIndex: self.index];
}
我已经调整了我的代码试图摆脱这个,包括将self.index初始化为0或1,并使用我提供的那种视图控制器,但这并没有导致它。有没有人见过这个或知道是什么导致它?我在SO上找不到任何东西,但也许我使用了错误的搜索词。
答案 0 :(得分:0)
您的问题可能是框架提前缓存了额外的数据,而您的代码的副作用导致了问题。您应该避免UIPageViewControllerDataSource
方法中的那些副作用:这些方法应该返回数据,而不执行任何其他操作。
执行此操作的正确方法是一些额外的工作,但它使代码更清晰,更易于维护。我会让每个页面成为UIViewController
的自定义子类,它知道它的页面索引,就像这样就足够了:
@interface MyPageViewControllerPage : UIViewController {
@property NSInteger index;
+(instancetype) pageWithIndex:(NSInteger)index;
}
@implementation MyPageViewControllerPage {
+(instancetype) pageWithIndex:(NSInteger)index {
MyPageViewControllerPage* page = [[MyPageViewControllerPage alloc]init];
page.index = index;
page.view.backgroundColor = BACKGROUND_COLOR;
page.view.userInteractionEnabled = NO;
}
}
然后在您的DataSource实现中,更新它以使用这个新类:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
MyPageViewControllerPage* page = (MyPageViewControllerPage*)viewController;
if (page.index <= 0) return nil;
else return [self viewControllerAtIndex: page.index - 1];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
MyPageViewControllerPage* page = (MyPageViewControllerPage*)viewController;
// Limit to the right number of pgaes, but allow one extra at the end to signal dismissal
if (page.index > self.numberOfPages) return nil;
else return [self viewControllerAtIndex: page.index + 1];
}
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index {
return [MyPageViewControllerPage pageWithIndex: index];
}
这样,UIKit框架可以为您管理一切。如果你想在到达最后一页时让页面VC被忽略,我会像{em}那样实现UIPageViewControllerDelegate
方法pageViewController:willTransitionToViewControllers:
:
-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers {
MyPageViewControllerPage* page = (MyPageViewControllerPage*)pendingViewControllers[0];
if (page.index == self.numberOfPages) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
确保您在界面构建器或viewDidLoad
中设置了委托(如果您还没有):
-(void)viewDidLoad {
// ... other initialization ...
self.pageViewController.delegate = self;
}