UIPageViewController Refresh Content and Bounce

0

Have a Page in my application that works with UIPageViewController, in it I implement the UIPageViewControllerDataSource. So I have a list of items that are listed in various views, the problem is that when I update this list, the items work perfectly, I can browse all items, including new items. The problem is that the Bounce (low balls) are the old value, that is, if it had 5 pages before and now 6 continues 5 balls. I did not find in the documentation any command that updates this information. Below is part of my class which is about UIPageViewControllerDataSource.

func loadPageView() {
    self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController;
    self.pageViewController.dataSource = self;
    self.pageViewController.delegate = self;

    let startVc = self.viewControllerAtIndex(0) as ContentViewController;
    let viewsControllers : [ContentViewController] = [startVc];

    self.pageViewController.setViewControllers(viewsControllers, direction: .Forward, animated: true, completion: nil);

    self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width , self.view.frame.size.height - 70);
    self.addChildViewController(self.pageViewController);
    self.view.addSubview(self.pageViewController.view);
    self.pageViewController.didMoveToParentViewController(self);
}


func viewControllerAtIndex(index: Int) -> ContentViewController {
    if((ExercicioDataSource.listaExercicios.count == 0) || (index >= ExercicioDataSource.listaExercicios.count)){
        return ContentViewController();
    }

    let vc : ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController;
    vc.dataContent = ExercicioDataSource.listaExercicios[index];
    vc.index = index;

    return vc;
}

// UI Page View Data Source

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    let vc = viewController as! ContentViewController;
    var index = vc.index as Int;

    if(index == 0 || index == NSNotFound){
        return nil;
    }

    index -= 1;
    return self.viewControllerAtIndex(index);
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    let vc = viewController as! ContentViewController;
    var index = vc.index as Int;
    if(index == NSNotFound){
        return nil;
    }

    index += 1;

    if(index == ExercicioDataSource.listaExercicios.count){
        return nil;
    }
    return self.viewControllerAtIndex(index);
}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return ExercicioDataSource.listaExercicios.count;
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return 0;
}
    
asked by anonymous 08.09.2016 / 17:38

1 answer

0

Hello! It is probably not automatically receiving data from the page view controller through the delegate; so whenever you add a new page, it does not detect changes. One question: if you delete a VC, does it still count as 5 instead of updating to 4 balls? Could you post the whole code including viewDidLoad and defined variables? Anyway, try this method:

First of all, make the Page Control outlet in the code.

@IBOutlet weak var pageControl: UIPageControl!

Now ...

  • In PageViewController:
  • Set the protocol:

    protocol PageViewControllerDelegate: class {
    
        //chamado quando o número de páginas for atualizado: 
        func pageViewController(pageViewController: UIPageViewController,
            didUpdatePageCount count: Int)
    
        //chamado quando o index for atualizado
        func pageViewController(pageViewController: UIPageViewController,
            didUpdatePageIndex index: Int)
    
    }
    

    Set a variable according to the new delegate PageViewController:

    weak var pageDelegate: PageViewControllerDelegate?
    

    Add in viewDidLoad ():

    pageDelegate?.pageViewController(self,
                didUpdatePageCount: ExercicioDataSource.listaExercicios.count)
    
  • No ContentViewController:
  • Add extension:

    extension ContentViewController: TutorialPageViewControllerDelegate {
    
    func pageViewController(pageViewController: UIPageViewController,
        didUpdatePageCount count: Int) {
            pageControl.numberOfPages = count
        }
    
        func tutorialPageViewController(tutorialPageViewController: TutorialPageViewController,
            didUpdatePageIndex index: Int) {
             index = vc.index
        }
    
    }
    
        
    22.09.2016 / 19:06