更新ViewController不更新PageViewController点

时间:2018-03-30 10:15:52

标签: ios swift cocoa-touch uipageviewcontroller

我正在尝试使用3个ContentViewControllers呈现一个PageViewController,用户可以从一个页面移动到另一个页面,但也可以使用计时器自动滚动到下一页。

第一个问题: 如何让Page View Controller正确更新页面底部的点。

我实现了三页PageViewController并具有基于计时器的自动化功能。计时器调用:

@objc func animation() {
    if counter < 2  {counter += 1}
    else {counter = 0}        
    self.pageViewController.setViewControllers([self.pageTutorialAtIndex(counter)], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
}

对setViewControllers的调用会更改视图。 但是,它不会更新页面底部的PageViewController点。需要做些什么来解决这个问题?

显然这个班级计数器没有得到当前显示的ViewContoller,这引出了我的第二个问题。

第二个问题: 如何正确实现从显示的页面获取当前索引?我试过了:

let current = self.pageTutorialAtIndex(0) as SplashContentViewController
var counter : Int = current.pageIndex!

认为这会读取当前页面但似乎只读取页面0。

class和viewDidLoad是:

class SplashViewController: UIViewController, UIPageViewControllerDataSource {

    var pageImages:NSArray!
    var pageTitles:[String]!
    var pageTexts:[String]!
    var pageViewController:UIPageViewController!
    var waitTimer : Timer!
    var counter : Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        pageImages = NSArray(objects:"sp_image1","sp_image2","sp_image3")
        pageTitles = [NSLocalizedString("sp_title1",comment: ""), NSLocalizedString("sp_title2", comment: ""), NSLocalizedString("sp_title3", comment: "")]
        pageTexts = [NSLocalizedString("sp_detail1",comment: ""), NSLocalizedString("sp_detail2", comment: ""), NSLocalizedString("sp_detail3", comment: "")]

        self.pageViewController = self.storyboard?.instantiateViewController(withIdentifier: "SplashPageViewController") as! UIPageViewController
        self.pageViewController.dataSource = self
        let initialContenViewController = self.pageTutorialAtIndex(0) as SplashContentViewController
        self.pageViewController.setViewControllers([initialContenViewController], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
        self.addChildViewController(self.pageViewController)
        self.view.addSubview(self.pageViewController.view)
        self.pageViewController.didMove(toParentViewController: self)
    }

使用实现委托方法:

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
    {
        let viewController = viewController as! SplashContentViewController
        var index = viewController.pageIndex as Int
        if(index == 0 || index == NSNotFound){return nil}
        index -= 1
        return self.pageTutorialAtIndex(index)
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
    {
        let viewController = viewController as! SplashContentViewController
        var index = viewController.pageIndex as Int
        if((index == NSNotFound)){return nil}
        index += 1
        if(index == pageImages.count){return nil}
        return self.pageTutorialAtIndex(index)
    }

    func presentationCount(for pageViewController: UIPageViewController) -> Int
    {
        return pageImages.count
    }

    func presentationIndex(for pageViewController: UIPageViewController) -> Int
    {
        return 0
    }

2 个答案:

答案 0 :(得分:3)

我似乎找到了一个可能的答案:

第1个问题:如何让Page View Controller正确更新页面底部的点。而不是返回0,返回当前计数器提供权利。

 func presentationIndex(for pageViewController: UIPageViewController) -> Int
{
    return counter
}

第二个问题:如何实现获取当前索引?在我的解决方案中,计数器变量标识当前页面。它没有从显示的页面正确获取索引,但它可以正常工作。

使用全局计数器变量实现:

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
{
    let viewController = viewController as! SplashContentViewController
    let index = viewController.pageIndex as Int

    if(index == NSNotFound){
        return nil
    }
    if(index > 0){
        counter = index - 1
        return self.pageTutorialAtIndex(counter)
    } else {
        counter = 0
        return nil
    }
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
{
    let viewController = viewController as! SplashContentViewController
    let index = viewController.pageIndex as Int
    if((index == NSNotFound)){
        return nil
    }
    if (index < pageImages.count - 1) {
        counter = index + 1
        return self.pageTutorialAtIndex(counter)
    } else {
        counter = pageImages.count - 1
        return nil
    }
}

答案 1 :(得分:0)

您好,您已经在以下位置设置了当前页面的索引:

viewControllerBefore和viewControllerAfter

只需创建一个全局变量并在每个函数中返回视图控制器之前存储索引值,例如

self.currentIndex = index 

然后返回有问题的视图控制器

return self.pageTutorialAtIndex(index)