Uipageviewcontroller Update Content and Bounce

Asked

Viewed 40 times

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 navigate all items, including new ones. The problem is that Bounce (bottom balls) are the old value, IE, 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 that deals with 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;
}

1 answer

0

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

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

@IBOutlet weak var pageControl: UIPageControl!

Now...

  1. In the Pageviewcontroller:

Define 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 to viewDidLoad():

pageDelegate?.pageViewController(self,
            didUpdatePageCount: ExercicioDataSource.listaExercicios.count)

  1. No Contentviewcontroller:

Add the extension:

extension ContentViewController: TutorialPageViewControllerDelegate {

func pageViewController(pageViewController: UIPageViewController,
    didUpdatePageCount count: Int) {
        pageControl.numberOfPages = count
    }

    func tutorialPageViewController(tutorialPageViewController: TutorialPageViewController,
        didUpdatePageIndex index: Int) {
         index = vc.index
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.