da1lbi3 da1lbi3 - 3 months ago 15
Swift Question

How to retrieve value from extension in view controller swift

I want to retrieve the value from the scrollViewDidScroll function at my view controller. It gives the right value back in the console so that's nice.

My view controller:

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 test: UIViewController {

var pageIndex: Int!

override func viewDidLoad()
{
super.viewDidLoad()

}

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

}


Where the view controller came from:

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
}

}


Is this possible?

Answer

You could do like this:

1. Replace your extension with a custom 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 is a BasePageViewController subclass

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")
    }
    ......
}
Comments