0

I'm creating a custom refresh control and I need to get some UIScrollViewDelegate methods from the UITableViewController,

What I was doing before is creating implementing UIScrollViewDelegate into my CustomRefreshControl and just overriding scrollViewMethods and passing the all the values inside my customRefresh control, but this approach is a bit ugly and not practical since I would have to do it for every class that applies this CustomRefreshControl so I tried to solve it with protocol extensions but it's not working...

class CustomRefreshControl: UIView {
    // initializer and all other properties here
    // ...
}

extension CustomRefreshControl: UIScrollViewDelegate {
    // Here my implementation inside custom refresh control so I can make it behave like I intend,
    // But since all scroll methods from UITableViewController are called in the controller itself and I could not figure out a way to send these events directly into my customRefresh
    // I implemented these methods here and just passing them from controller to my customRefresh

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        // doing some calculations here
    }

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        // doing some calculations here
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // doing some calculations here
    }
}

How I was doing before on viewController side...

class RefreshingTableViewController: UITableViewController {
    lazy var configuration: CustomRefreshControl.Configurations = {
        return CustomRefreshControl.Configurations(threshold: 160, scrollView: self, refreshAction: refreshData)
    }()

    lazy var refresh = CustomRefreshControl(configurations: configuration)

    // All other properties and delegate and datasource implementations
    //...
}

extension RefreshingTableViewController {
    // That was my previous implementation But this solution did please so much
    // Because I have to implement this in every single viewController that wants to use this CustomRefreshControl

    override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        refresh.scrollViewWillBeginDragging(scrollView)
    }

    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        refresh.scrollViewDidScroll(scrollView)
    }

    override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        refresh.scrollViewWillEndDragging(scrollView, withVelocity velocity: velocity, targetContentOffset: targetContentOffset)
    }
}

So the solution I'm trying to make work right now is a solution with protocols, but I don't know if it's even possible, it is as follows...

protocol CustomRefreshManagerProtocol where Self: UIScrollViewDelegate {
    var customRefresh: CustomRefreshControl { get }
}

extension CustomRefreshManagerProtocol  {
    // Here I'm trying to implement the UIScrollViewDelegate methods, since I specifing that Self: UIScrollViewDelegate I thought it might work
    // But none of these functions are being called, so that's what I'm trying to get to work without success.

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        customRefresh.scrollViewWillBeginDragging(scrollView)
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        customRefresh.scrollViewDidScroll(scrollView)
    }

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        customRefresh.scrollViewWillEndDragging(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset)
    }
}

class RefreshingTableViewController: UITableViewController, CustomRefreshManagerProtocol {
    lazy var refresh = CustomRefreshControl(configurations: configuration)
    var customRefresh: CustomRefreshControl { refresh }
    // ...
}

Anyone has any idea how to make this work or why it's not working?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lucas
  • 746
  • 8
  • 23

1 Answers1

0

Extensions cannot override methods at all (it sometimes works, but it's not defined behavior). So the answer is no.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610