获取动态页面uiviewcontroller的真实索引

时间:2016-08-19 23:22:39

标签: swift

我怎样才能得到真实的'活动页面的当前索引?我已经尝试了堆栈溢出的所有解决方案,没有运气。所以vc.pageIndex给了我错误的值。我该如何解决这个问题?

我的代码:

import UIKit



class PageControl: BasePageViewController, UIPageViewControllerDataSource {

    var pageViewController: UIPageViewController!
    var pageTitles: [[String]]!
    var pageImages: NSArray!
    var complete : CGFloat = 0.0;


    override func viewDidLoad(){
        super.viewDidLoad()

        HealthKit().recentSteps() { steps, error in
            self.pageTitles = steps;


            self.pageViewController = self
            self.pageViewController.dataSource = self

            let startVC = self.viewControllerAtIndex(0) as StepsDetailViewController
            let viewControllers = NSArray(object: startVC)

            dispatch_async(dispatch_get_main_queue(), {

            self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)

            })
        }

    }



    override func percentCompleteDidChange() {
        if let percentComplete = self.percentComplete {
            complete = percentComplete

            let vc: StepsDetailViewController = self.storyboard?.instantiateViewControllerWithIdentifier("StepsDetailViewController") as! StepsDetailViewController

            vc.updateProgressComplete(percentComplete, index: 0)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    func viewControllerAtIndex(index: Int) -> StepsDetailViewController
    {
        if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
            return StepsDetailViewController()
        }

        let vc: StepsDetailViewController = self.storyboard?.instantiateViewControllerWithIdentifier("StepsDetailViewController") as! StepsDetailViewController

            vc.titleText = self.pageTitles[index][0]
            vc.stepData = self.pageTitles[index][1]
            vc.pageIndex = index

            return vc
    }


    // MARK: - Page View Controller Data Source
    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
    {

        let vc = viewController as! StepsDetailViewController
        var index = vc.pageIndex 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! StepsDetailViewController
        var index = vc.pageIndex as Int

        if (index == NSNotFound){
            return nil
        }

        index += 1

        if (index == self.pageTitles.count)
        {
            return nil
        }

        return self.viewControllerAtIndex(index)

    }
}

0 个答案:

没有答案