Overriding delegate property of UIScrollView in Swift (like UICollectionView does)
Asked Answered
S

6

39

UIScrollView has a delegate property which conforms to UIScrollViewDelegate

protocol UIScrollViewDelegate : NSObjectProtocol {
    //...
}
class UIScrollView : UIView, NSCoding {
    unowned(unsafe) var delegate: UIScrollViewDelegate?
    //...
}

UICollectionView overrides this property with a different type UICollectionViewDelegate

protocol UICollectionViewDelegate : UIScrollViewDelegate, NSObjectProtocol {
   //...
}

class UICollectionView : UIScrollView {
     unowned(unsafe) var delegate: UICollectionViewDelegate?
   //...
}

When I try to override UIScrollViews delegate with my protocol like so:

protocol MyScrollViewDelegate : UIScrollViewDelegate, NSObjectProtocol {
    //...
}

class MyScrollView: UIScrollView {
    unowned(unsafe) var delegate: MyScrollViewDelegate?

}

the compiler gives me two warnings:

  • Property 'delegate' with type 'MyScrollViewDelegate?' cannot override a property with type 'UIScrollViewDelegate?'
  • 'unowned' cannot be applied to non-class type 'MyScrollViewDelegate?'

How can I subclass UIScrollView and override type of delegate property (i.e. use a custom delegate protocol) ?

Schell answered 8/9, 2014 at 12:50 Comment(0)
S
21

I think overriding an inherited property is something that's possible in Objective-C but not (at least currently) in Swift. The way I've handled this is to declare a separate delegate as a computed property of the correct type that gets and sets the actual delegate:

@objc protocol MyScrollViewDelegate : UIScrollViewDelegate, NSObjectProtocol {
    func myHeight() -> CGFloat
    // ...
}

class MyScrollView: UIScrollView {
    var myDelegate: MyScrollViewDelegate? {
        get { return self.delegate as? MyScrollViewDelegate }
        set { self.delegate = newValue }
    }
}

This way anything that calls the scroll view delegate normally still works, and you can call your particular delegate methods on self.myDelegate, like this:

if let height = self.myDelegate?.myHeight() {
    // ...
}
Swellfish answered 8/9, 2014 at 15:7 Comment(1)
This doesn't work on swift 2.1, any update to this approach?Ceremonial
D
7

You can do like this:

protocol ExtendedUIScrollViewDelegate: UIScrollViewDelegate {
    func someNewFunction()
}

class CustomScrollView: UIScrollView {

    weak var myDelegate: ExtendedScrollViewDelegate?
    override weak var delegate: UIScrollViewDelegate? {
        didSet {
            myDelegate = delegate as? ExtendedScrollViewDelegate
        }
    }
}

Hope this helps

Drench answered 7/9, 2016 at 3:53 Comment(0)
F
4

My favoured method personally is not to subclass scrollviews directly but to make a UIView subclass containing and acting as delegate for a separate scrollview, then forward that scrollview's delegate messages on to the UIView subclass's own delegate where necessary. This also allows for the adding of custom controls outside of the area defined by the scroll view. It may seem a little inelegant compared to a direct subclass, but it does at least avoid unpleasant hacks.

Fash answered 28/9, 2014 at 8:30 Comment(1)
could you please be more descriptive or post some code?, this looks interesting...Delainedelainey
U
4

Here is a solution for changing the type of the overriding properties in Swift. It is especially useful when you need to extend protocols of delegates.

@objc protocol ExtendedUIScrollViewDelegate: UIScrollViewDelegate {
     func someNewFunction()
}

class CustomScrollView: UIScrollView {

    weak var delegateInterceptor: ExtendedScrollViewDelegate?
    override var delegate: UIScrollViewDelegate! {
        didSet {
            if let newValue = delegate {
                let castedDelegate = unsafeBitCast(delegate, ExtendedScrollViewDelegate.self)
                delegateInterceptor = castedDelegate
            }
            else {
                delegateInterceptor = nil
            }
        }
    }
}

This works as tested with Swift version 1.2. I hope it helps.

Undaunted answered 27/5, 2015 at 16:11 Comment(0)
S
1

You can override get and set method by declare function like:

func setDelegate(delegate:UITableViewDelegate?){
     self.delegateInterceptor = delegate;
}

swift compiler the property to method as Objective-c does.

Sac answered 17/9, 2014 at 2:19 Comment(1)
This did not work for me. I have a pure Swift app. After adding this setter method to my subclass of UITextView, doing myTextView.delegate = self from the delegate (my view controller) didn't call the setter method I added. I didn't try the getter method.Folk
T
1

Consider the following situation:

class BaseProp {}

class Base {
    var prop: BaseProp
}

Then if you do this:

class DerivedProp: BaseProp {}

class Derived: Base {
    override var prop: DerivedProp
}

Then if would break the subclassing principles (namely, the Liskov Substitution Principle). Basically what you are doing is limiting the scope of "var prop" from wider "BaseProp" type to a more narrow "DerivedProp" type. Then this kind of code would be possible, which does not make sense:

class UnrelatedProp: BaseProp {}

let derived = Derived()
let base = derived as Base
base.prop = UnrelatedProp()

Note that we are assigning an instance of UnrelatedProp to the property, which does not make sense for the Derived instance which we actually operate with. ObjectiveC allows such kind of ambiguity, but Swift doesn't.

Thusly answered 3/10, 2018 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.