pageViewController setViewControllers crashes with

2020-02-07 19:52发布

I know that there are several other questions like this, but i couldn't find a solution for my problem. I use a pageViewController which displays different ViewControllers. Every time the pageViewController moves forward I check the input of the lastPage. If it's wrong the pageViewController should go back to that page by using the setViewController method. Without this method everything works fine but if I try to use it the app crashes with the following exception:

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'

And here's my code:

- (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

        }
    }
}

As I already said. I searched a lot and implemented some solutions but nothing helped. Thanks.

标签: xcode
9条回答
▲ chillily
2楼-- · 2020-02-07 20:32

Well it's 2018 and the bug is still around. I tried all the solutions above, but none worked for me. And after many tries, setting the animation parameter to false, was what worked for me in swift 4

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

Then it was just a matter of setting a custom transition. Hope it helps others in the same situation.

查看更多
小情绪 Triste *
3楼-- · 2020-02-07 20:33

I get this problem today.
I start an animation when UIPageController switch viewController.

- (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion;

Program will crash if animation:YES,but if animation:NO it will be fine.

I guess we cant do two animations as the same time. But if you want animation:YES,you can do your other animation after switch viewControlle's,such as use GCD after.

查看更多
爷、活的狠高调
4楼-- · 2020-02-07 20:34

In addition to

DispatchQueue.main.async {
        self.pageViewController?.setViewControllers([self.initialViewControllerAtIndex(index: 0)!], direction: .forward, animated: false, completion: nil)
    }

I had to also place this following code within a DispatchQueue.main.async

DispatchQueue.main.async {
           for v in self.pageViewController!.view.subviews {
                if let sub = v as? UIScrollView {
                    sub.isScrollEnabled = enable
                }
            }
}

Because I was enable/disabling scrolling until users completed filling out values on previous pages, before allowing them to go to the next page

查看更多
登录 后发表回答