hughesdan
hughesdan

Reputation: 2851

How do you access scrollViewDidScroll from a UIViewController

I have a view controller with a UIScrollView embedded as a sub view. I embed it as follows:

CaptionViewController : UIViewController

Inside ViewDidLoad

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
scrollView.contentMode = (UIViewContentModeScaleAspectFit);
scrollView.contentSize = CGSizeMake(3200, 320);
scrollView.pagingEnabled = YES;
[self.view addSubview:scrollView];

Next I'm trying to wire up the scrollViewDidScroll event so I can execute some code each time the user swipes the scroll view. However, I can figure out how to access this event. I think the answer lies in delegation somehow. I tried importing UIScrollView.h and setting the scrollView delegate to the CaptionViewController as follows:

[scrollView setDelegate:self]

Still I cannot access scrollViewDidScroll. Can someone please point me in the right direction?

Upvotes: 3

Views: 1537

Answers (1)

rmaddy
rmaddy

Reputation: 318824

In addition to setting the delegate of the scroll view, you have to implement the delegate method in your CaptionViewController.m.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // handle scroll event
}

Also make sure your CaptionViewController conforms to the UIScrollViewDelegate protocol. In the .m file:

@interface CaptionViewController () <UIScrollViewDelegate>

@end

There is no need to import UIScrollView.h. You get this already.

Upvotes: 4

Related Questions