pageViewController setViewControllers崩溃"无效参数不满意:[views count] == 3"

 常德锦江-余欢 发布于 2023-01-12 09:48

我知道还有其他几个这样的问题,但我无法找到解决问题的方法.我使用了一个显示不同ViewControllers的pageViewController.每次pageViewController向前移动时,我都会检查lastPage的输入.如果错误,pageViewController应该使用setViewController方法返回到该页面.没有这个方法一切正常,但如果我尝试使用它,应用程序崩溃与以下异常:

19:48:25.596 Phook[23579:60b] *** Assertion failure in -[_UIQueuingScrollView _replaceViews:updatingContents:adjustContentInsets:animated:], /SourceCache/UIKit_Sim/UIKit-2935.137/_UIQueuingScrollView.m:383  
2014-06-02 19:48:25.600 Phook[23579:60b] *** Terminating app due to uncaught   exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying:   [views count] == 3'

这是我的代码:

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished
   previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    if (completed && finished)
    {

        RegisterUserPageContent* lastPage = (RegisterUserPageContent*)previousViewControllers[ previousViewControllers.count-1];
        int lastIndex   = lastPage.pageIndex;
        int newIndex    = ((RegisterUserPageContent*)[self.pageViewController.viewControllers objectAtIndex:0]).pageIndex;

        if (newIndex > lastIndex)
        {   // Moved Forward
            if(!lastPage.testInput)
            {
                [self.pageViewController setViewControllers:@[ [self.storyboard instantiateViewControllerWithIdentifier:_pageStoryBoardIDs[0]] ]
                                         direction:UIPageViewControllerNavigationDirectionReverse
                                         animated:YES completion:nil];


            }
        }
        else
        {   // Moved Reverse

        }
    }
}

正如我已经说过的那样 我搜索了很多并实施了一些解决方案但没有任何帮助.谢谢.

3 个回答
  • 我遇到了同样的问题,并且能够使用这个答案的提示解决它/sf/ask/17360801/.简单地放置setViewControllers:direction:animated:completion:主队列中dispatch_async块内的代码为我修复了它.对你来说这看起来像

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.pageViewController setViewControllers:@[ [self.storyboard instantiateViewControllerWithIdentifier:_pageStoryBoardIDs[0]] ]
                                         direction:UIPageViewControllerNavigationDirectionReverse
                                         animated:YES completion:nil];
    });
    

    希望能帮助到你!

    2023-01-12 09:54 回答
  • 好吧,这是2018年,错误仍然存​​在。我尝试了上述所有解决方案,但没有一个对我有用。经过多次尝试,将animation参数设置为false,这是我迅速完成的工作4

    let myViewController : UIViewController = orderedViewControllers[vcIndex]
    setViewControllers([myViewController], direction: .forward, animated: false, completion: nil);
    

    然后,只需设置自定义过渡即可。希望它可以帮助处于相同情况的其他人。

    2023-01-12 09:55 回答
  • 进入同样的问题.通过在首次加载时在UIPageViewController上显式设置第一个内容控制器来解决它(即:在'viewDidLoad'内).

    // create first page
    UIViewController* firstContentPageController = [self contentControllerAtIndex: 0];
    [_paginationController
        setViewControllers: @[firstContentPageController]
        direction: UIPageViewControllerNavigationDirectionForward
        animated: NO
        completion: nil];
    

    其中'contentControllerAtIndex:'是一个创建内容控制器的简单助手方法.我还在两个委托方法中使用它,以便为给定页面返回适当的控制器.

    - (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
        NSInteger index = (NSInteger)((MyContentController*)viewController).pageIndex;
    
        return [self contentControllerAtIndex: index - 1];
    }
    
    
    - (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
        NSInteger index = (NSInteger)((MyContentController*)viewController).pageIndex;
    
        return [self contentControllerAtIndex: index + 1];
    }
    
    
    - (MyContentController*)contentControllerAtIndex: (NSInteger)index {
        if (index < 0 || _pagesContent.count <= index)
            return nil;
    
        // create view controller
        MyContentController* contentController = [self.storyboard instantiateViewControllerWithIdentifier: @"MyContentController"];
        contentController.pageIndex = index;
        contentController.content = [_pagesContent objectAtIndex: index];
    
        return contentController;
    }
    

    这样做的原因是UIPageViewControllerDataSource协议被设计为只有拉动上一个/下一个内容控制器的方法,而不是在特定索引处拉动单个控制器.这是一个奇怪的设计决定,但需要注意的是,当首次加载组件时,不必由Cocoa调用,而是必须手动设置其起始状态.笨拙的框架设计恕我直言

    2023-01-12 09:55 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有