Search code examples
iosdelegatesuiscrollviewdelegate

UIScrollView's delegate methods not firing


I can't get the viewDidScroll method to fire in my custom class... I know, I know this has been in multiple stackoverflow posts... I have read dozens... Please help!

Following directions from other S.O. posts,

I have added the protocol in my .h file:

@interface ISCByStyleView : UIViewController <UIScrollViewDelegate>

I have declared a "delegate" property in the .h file of this custom class:

@property (nonatomic, strong) id delegate;

I have referenced the delegate in the .m file like this:

self.delegate=self;

But when I scroll the Scroll View, the method viewDidSCroll WILL NOT fire.

Here is the .h file:

#import <UIKit/UIKit.h>
#import "FMDatabase.h"
#import "FMResultSet.h"
#import "ISCAppDelegate.h"

@interface ISCByStyleView : UIViewController <UIScrollViewDelegate>

@property (nonatomic, strong) id delegate;

@property (weak, nonatomic) IBOutlet UIView *iboStylesView;

@end

And here are the important parts of the .m

#import "ISCByStyleView.h"
#import "ISCTableView.h"

@interface ISCByStyleView ()

@property NSNumber *selectedStyle;
@property NSNumber *selectedQuery;
@property NSTimer *ssTimer;

@end

@implementation ISCByStyleView

- (void)viewDidLoad {
    [super viewDidLoad];

    self.delegate=self;

...

- (void) resetTimer {
    [self.ssTimer invalidate];
    self.ssTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self  selector:@selector(launchScreenSaver) userInfo:Nil repeats:NO];
NSLog (@"Timer Reset");
}

//Reset with Scrolling
- (void) scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog (@"ByStyleView: scrollDidScroll event");
    [self resetTimer];

}

Solution

  • ISCByStyleView is a UIViewController and so you don't want to set self's delegate. Instead, you need to set your UIScrollView's delegate to self, ex:

    self.scrollview.delegate = self;
    

    And declaring the delegate property in your .h using this line: @property (nonatomic, strong) id delegate; is unnecessary.

    Edit: You could also set your UIScrollView's delegate entirely within the Interface Builder if that's where you've created it like so:

    Step #1:

    enter image description here

    Step #2:

    enter image description here