在UIPageViewController中加载多个View控制器 - 滑动显示空白屏幕

时间:2014-09-18 02:31:24

标签: ios objective-c cocoa-touch uiviewcontroller uipageviewcontroller

我在UIPageViewController中加载多个UIViewControllers,每个UIViewController都有自己的导航栏。我已经为UIPageViewController实现了所有必需的数据源和委托方法,并且它正确显示。

但是当我水平滚动视图控制器时,更常见的是,我看到一个白色的空白屏幕,而不是我的视图控制器。以下是我的代码。

任何线索都可能出错。

PS:我尝试通过移除viewControllerForRequestID方法中的IF条件并每次都返回一个新控制器。这可以工作,但最后我得到一个视图控制器在分页中出现两次。

- (UIViewController *)pageViewController:(UIPageViewController *)iPageViewController viewControllerBeforeViewController:(UIViewController *)iViewController {
    [self.pageControl setCurrentPage:self.selectedRequestIndex];

    if (self.selectedRequestIndex == 0) {
        return nil;
    }

    NSUInteger aNewSelectedRequestIndex = self.selectedRequestIndex - 1;
    NSString *aPreviousRequestID = [self.paginationRequestIDList[aNewSelectedRequestIndex] stringValue];

    self.selectedRequestIndex = aNewSelectedRequestIndex;
    self.selectedRequestID = aPreviousRequestID;

    return [self viewControllerForRequestID:self.selectedRequestID];
}


- (UIViewController *)pageViewController:(UIPageViewController *)iPageViewController viewControllerAfterViewController:(UIViewController *)iViewController {
    [self.pageControl setCurrentPage:self.selectedRequestIndex];

    if (self.selectedRequestIndex == NSNotFound || self.selectedRequestIndex >= self.paginationRequestIDList.count - 1) {
        return nil;
    }

    NSUInteger aNewSelectedRequestIndex = self.selectedRequestIndex + 1;
    NSString *aNextRequestID = self.paginationRequestIDList[aNewSelectedRequestIndex];

    self.selectedRequestIndex = aNewSelectedRequestIndex;
    self.selectedRequestID = aNextRequestID;

    return [self viewControllerForRequestID:self.selectedRequestID];
}


- (MyNavigationViewController *)viewControllerForRequestID:(id)iRequestID {
    NSString *aRequestID = iRequestID;

    // Safe check to avoid numbers in request ID
    if (![iRequestID isKindOfClass:[NSString class]]) {
        aRequestID = [iRequestID stringValue];
    }

    @synchronized (self) {
        if ([self.viewControllers containsObjectForKey:iRequestID]) {
            MyNavigationViewController *aNavigationController = self.viewControllers[iRequestID];
            return aNavigationController;
        }

        MyDataModel *aRequestData = self.savedRequestData[iRequestID];
        MyDataModelController *aRequestInfoController = [[MyDataModelController alloc] initWithRequestData:aRequestData];
        aRequestInfoController.delegate = self;
        MyNavigationViewController *aNavigationController = [[MyNavigationViewController alloc] initWithRootViewController:aRequestInfoController];

        [self.viewControllers setObject:aNavigationController forKey:iRequestID];

        return aNavigationController;
    }
}

0 个答案:

没有答案