Here is what I want to achieve:
I want to subclass an UIScrollView to have additional functionality. This subclass should be able to react on scrolling, so i have to
Don't subclass it, encapsulate it instead ;)
Make a new UIView subclass - your header file would look like :
@interface MySubclass : UIView
@end
And just have a UIScrollView as a subview - your .m file would look like :
@interface MySubclass ()
@property (nonatomic, strong, readonly) UIScrollView *scrollView;
@end
@implementation MySubClass
@synthesize scrollView = _scrollView;
- (UIScrollView *)scrollView {
if (nil == _scrollView) {
_scrollView = [UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.delegate = self;
[self addSubView:_scrollView];
}
return _scrollView;
}
...
your code here
...
@end
Inside your subclass, just always use self.scrollView
and it will create the scroll view the first time you ask for it.
This has the benefit of hiding the scroll view completely from anyone using your MySubClass
- if you needed to change how it worked behind the scenes (i.e. change from a scroll view to a web view) it would be very easy to do :)
It also means that no-one can alter how you want the scroll view to behave :)
PS I've assumed ARC - change strong
to retain
and add dealloc
if necessary :)
EDIT
If you want your class to behave exactly as a UIScrollView then you could try this adding this method (taken from these docs and untested!) :
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ([self.scrollView respondsToSelector:anInvocation.selector])
[anInvocation invokeWithTarget:self.scrollView];
else
[super forwardInvocation:anInvocation];
}