我想从视图控制器的scrollViewDidScroll函数中检索值。它在控制台中返回正确的值,所以这很好。
我的视图控制器:

import UIKit

extension UIPageViewController: UIScrollViewDelegate {
    public override func viewDidLoad() {
        super.viewDidLoad()
        for subView in view.subviews {
            if subView is UIScrollView {
                (subView as! UIScrollView).delegate = self
            }
        }
    }

    public func scrollViewDidScroll(scrollView: UIScrollView) {
        let point = scrollView.contentOffset
        var percentComplete: CGFloat
        percentComplete = fabs(point.x - view.frame.size.width)/view.frame.size.width
        print("percentComplete: ", percentComplete)


    }

}

class StepsDetailViewController: UIViewController {

      var pageIndex: Int!

    override func viewDidLoad()
    {
        super.viewDidLoad()

    }

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

}

视图控制器的来源:
import UIKit

class PageControl: UIPageViewController, UIPageViewControllerDataSource {

var pageViewController: UIPageViewController!
var pageTitles: [[String]]!

override func viewDidLoad(){
    super.viewDidLoad()

    testC().getRecent() { result, error in
        self.pageTitles = result;

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

        let startVC = self.viewControllerAtIndex(0) as StepsDetailViewController

        dispatch_async(dispatch_get_main_queue(), {

        let viewControllers = NSArray(object: startVC)


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

        })
    }

}

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.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)

}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
    return self.pageTitles.count
}

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

}

这可能吗?

最佳答案

你可以这样做:
一。将您的分机替换为自定义的UIPageViewController

class BasePageViewController: UIPageViewController, UIScrollViewDelegate {

    var percentComplete: CGFloat = 0.0 {
        didSet { self.percentCompleteDidChange() }
    }

    func percentCompleteDidChange() {
        print("percentCompleteDidChange")
    }

    public override func viewDidLoad() {
        super.viewDidLoad()
        for subView in view.subviews {
            if subView is UIScrollView {
                (subView as! UIScrollView).delegate = self
            }
        }
    }

    public func scrollViewDidScroll(scrollView: UIScrollView) {
        let point = scrollView.contentOffset
        self.percentComplete = fabs(point.x - view.frame.size.width)/view.frame.size.width
        print("percentComplete: ", percentComplete)
    }
}

2。PageControl是BasePageViewController子类
class PageControl: BasePageViewController, UIPageViewControllerDataSource {
    // here you have the access to percentComplete of superclass
    func printPercentComplete() {
        if let percentComplete = self.percentComplete {
            print(percentComplete)
        }
    }

    override func percentCompleteDidChange() {
        print("percentCompleteDidChange from child")
    }
    ......
}

关于swift - 如何从 View Controller Swift的扩展中检索值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39043995/

10-08 21:44