2

I found some answer that quite solve my questions, but seems that I'm missing some steps. I'm just trying to intercept the "ViewDidBlablabla" events of a ScrollView, so I created a custom UIScrollView class.

MyScrollView.m

#import "MyScrollView.h"

@implementation MyScrollView

- (id)initWithFrame:(CGRect)frame{
    NSLog(@"initWithFrame");
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView{
    NSLog(@"scrollViewWillBeginDragging");
    //scrollView.contentOffset
}

-(void)scrollViewDidScroll:(UIScrollView*)scrollView{
    NSLog(@"scrollViewDidScroll");
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    NSLog(@"scrollViewDidEndScrollingAnimation");
}

@end

MyScrollView.h

#import <UIKit/UIKit.h>

@interface MyScrollView : UIScrollView <UIScrollViewDelegate>

@end

When I alloc my custom Scroll View I do get the console message "initWithFrame", but there's no way to get to the other events. What am I missing?

If you need some other pieces of code feel free to ask, but since I get to my custom "initWithFrame" method, I suppose that the error should be here.

Alberto Schiariti
  • 1,546
  • 2
  • 15
  • 31

1 Answers1

4

Try setting your view as the delegate of itself:

if (self) {
    // Initialization code
    self.delegate = self;
}

Technically, you do not need to inherit UIScrollView to respond to scrolling events: it is sufficient to set the delegate to an implementation of <UIScrollViewDelegate>.

Also, the scrollViewDidEndScrollingAnimation: method has been gone for about two years, so it is not going to be called on modern iOS installations.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Oooh! Thanks! That was the point that I missed. Now it is working, but I maybe don't get the second part of your answer. Can you be a bit more clear? It's only one month that I'm using Objective-C :D – Alberto Schiariti May 01 '12 at 15:40
  • @AlbertoSchiariti in Cocoa, there are two major ways that you can go about extending functionality of a class: you can derive from it, which you did, or you can give an unrelated object to it as a delegate. For example, you could derive your view from `UIView`, not from `UIScrollView`, and it would have worked as well. Here is a [link](http://stackoverflow.com/a/1045854/335858) to an excellent answer about Objective C delegates here on Stack Overflow. – Sergey Kalinichenko May 01 '12 at 15:48
  • Ok, perfect! The fact is that I before tried to do as you said, but for some reason I couldn't, so I thought this was the only way to extend it. Thanks for the clear explanation. – Alberto Schiariti May 01 '12 at 15:56