iOS:对<uitableviewcontroller>的开始/结束外观转换的不平衡调用

时间:2015-09-18 12:09:23

标签: ios objective-c uitableview uipageviewcontroller

我有UIPageViewController,我动态创建3 UITableViewController

!!!!!仅当我使用"Partial Curl"转换样式时才会出现此问题。

如果我过快地滚动UIPageViewController,我会收到以下错误消息:

  

&#34;不平衡调用开始/结束外观转换   &#34;

我像这样创建UITableViewController

- (void)createTable {

CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

_tableViewController1 = [[UITableViewController alloc] init];
_tableViewController2 = [[UITableViewController alloc] init];
_tableViewController3 = [[UITableViewController alloc] init];

_tableViewController1.tableView.frame = frame;
_tableViewController2.tableView.frame = frame;
_tableViewController3.tableView.frame = frame;

_tableViewController1.tableView.backgroundColor = [UIColor greenColor];
_tableViewController2.tableView.backgroundColor = [UIColor redColor];
_tableViewController3.tableView.backgroundColor = [UIColor blueColor];

_viewControllersArray = @[_tableViewController1, _tableViewController2, _tableViewController3];
[self setViewControllers:@[_tableViewController1]
               direction:UIPageViewControllerNavigationDirectionForward
               animated:NO completion:nil];
}


- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{

    NSUInteger currentIndex = [_viewControllersArray indexOfObject:viewController];
    currentIndex--;

    if (currentIndex == -1) {
            currentIndex = 2;
            [self reloadTableView];
    }
    return [_viewControllersArray objectAtIndex:(currentIndex)];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{

    NSUInteger currentIndex = [_viewControllersArray indexOfObject:viewController];
    currentIndex++;

    if (currentIndex == 3) {
        currentIndex = 0;
        [self reloadTableView];
    }
    return [_viewControllersArray objectAtIndex:(currentIndex)];
}

2 个答案:

答案 0 :(得分:1)

问题是init还没有完成,但它将转到下一页。你应该添加一个标志来监控动画过程。

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{
if (completed || finished)
    _pageIsAnimating = NO;
}

-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers{
_pageIsAnimating = YES;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
if (_pageIsAnimating){
    return nil;
}

答案 1 :(得分:0)

我以为我会添加swift 2.2等价物:

class ExamplePageViewController: UIPageViewController {
    private var isAnimating = false
    // more codes follow

    override func viewDidLoad() {
        super.viewDidLoad()

        dataSource = self
        delegate = self
    }
}

// Data Source
extension ExamplePageViewController: UIPageViewControllerDataSource
{
    func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
        isAnimating = true
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        if isAnimating {
            return nil
        }

        // more codes follow
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        if isAnimating {
            return nil
        }

        // more codes follow
    }
}

// Delegate
extension ExamplePageViewController: UIPageViewControllerDelegate {
    func pageViewController(pageViewController: UIPageViewController,
                            didFinishAnimating finished: Bool,
                            previousViewControllers: [UIViewController],
                            transitionCompleted completed: Bool) {

        if (completed || finished) {
            isAnimating = false
        }

        // more codes follow
    }
}