使用不同iPhone设备的不同pageIndex

时间:2015-09-15 07:48:36

标签: ios objective-c iphone xcode

使用PageViewController我的jumpToPage逻辑有问题。我实现了如下所示的逻辑,它在iPhone 5S / 6/6 Plus和iPad上工作正常......但是在iPhone 5C / 5 / 4S及以下版本中我的方法将无法执行因为pageIndex不同...这是我的代码pageJump:

-(void)gotoPage:(int)index{


SinglePageViewController *viewController = [self viewControllerAtIndex:index];

UIPageViewControllerNavigationDirection direction;
if(_curIndex <= index){
    direction = UIPageViewControllerNavigationDirectionForward;
}
else
{
    direction = UIPageViewControllerNavigationDirectionReverse;
}


if(_curIndex < index)
{
    for (int i = 0; i <= index; i++)
    {
        if (i == index) {
            [self.pageViewController setViewControllers:@[viewController]
                                      direction:direction
                                       animated:YES
                                     completion:nil];
        }
        else
        {
            [self.pageViewController setViewControllers:@[[self viewControllerAtIndex:i]]
                                      direction:direction
                                       animated:NO
                                     completion:nil];

        }
    }
}
else
{
    for (int i = _curIndex; i >= index; i--)
    {
        if (i == index) {
            [self.pageViewController setViewControllers:@[viewController]
                                      direction:direction
                                       animated:YES
                                     completion:nil];
        }
        else
        {
            [self.pageViewController setViewControllers:@[[self viewControllerAtIndex:i]]
                                      direction:direction
                                       animated:NO
                                     completion:nil];

        }
    }
}

_curIndex = index;

}

好的,这个逻辑有效...这是检查当前pageIndex并调用这个gotoPage方法的方法:

-(void)refreshPagesWith:(Articles *)article{
    NSUInteger pageIndex = -1;

for (int i = 0; i < self.pages.count; i ++) {
    Pages *page = [self.pages objectAtIndex:i];
    if (page.articleID == article.articleID) {
        pageIndex = i;
        break;
    }
}


if (pageIndex != -1) {
    NSLog(@"THEORY: Maybe pageIndex IS -1 on iPhone 5 and below!!!");
    [self gotoPage:pageIndex];
}

好了,这就是问题...在iPhone 5S / 6 / 6Plus上,pageIndex不是-1,方法成功执行,一切正常......但由于某种原因,在iPhone 5C及以下版本中,pageIndex总是 - 1,方法不会执行...如果我试着[self gotoPage:pageIndex];应用程序崩溃...任何人都可以帮我改变我的逻辑吗?!?最诚挚的问候,

1 个答案:

答案 0 :(得分:0)

好吧,似乎我自己解决了我的这个问题,我只是想要一些有同样问题的人知道我做了什么......

出于某种原因,这行代码:if (page.articleID == article.articleID)不比较iPhone 5 / 4S模拟器中的对象......我只是改为if ([page.articleID isEqual: article.articleID]),现在它适用于所有设备... < / p>

请注意==比较指针,isEqual:比较对象...我希望将来这个答案能帮助遇到同样问题的每个人。