views:

577

answers:

2

The answer to this is probably right under my nose, but I am not seeing it. Maybe someone here could help.

I have a scrollView that allows for vertical scrolling. I set it up:

[clefScrollView addSubview:clefView];
[clefScrollView setContentSize:CGSizeMake(clefView.frame.size.width, clefView.frame.size.height)];
clefScrollView.showsVerticalScrollIndicator = YES;
clefScrollView.showsHorizontalScrollIndicator = NO;
clefScrollView.delegate = self;

I have the following methods included in the same file, in order to support the UIScrollViewDelegate protocol:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"%f %f", scrollView.contentOffset.y, scrollView.contentSize.height);
}

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

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

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

In addition, the .h file for my class includes the protocol:

@interface ClefNotesViewController : UIViewController <UIActionSheetDelegate,UIScrollViewDelegate,DoneWithVCDelegate> {

The problem is that no matter what, the following two are never called:

    -scrollViewDidEndDecelerating
    -scrollViewDidScroll

The other two methods do get called in response to different events. Given that two of the protocol methods are getting called, I assume that I have correctly set the delegate to my self. So, what is the problem?

Thanks!

Update: I found the problem, although haven't still figured out how to resolve it. My class spans two files. In the second file, a different scrollView is implementing -scrollViewDidEndDecelerating -scrollViewDidScroll

THe question is, how can I define two different sets of methods for two different UIScrollViews, in the same class?

I could try to handle two scrollViews with the same delegate methods, but that's ugly since I won't be able to keep each set of delegates with the scrollView's file. I could also split my class. Is there another way?

+1  A: 

THe question is, how can I define two different sets of methods for two different UIScrollViews, in the same class?

The delegate methods accept a scrollview objects as their parameter. You need to check which scrollview is being passed and respond accordingly. You can set each scrollview's tag property in order to identify them.

TechZen
yes, that does work. What I'd like, which I guess doesn't happen often, is to be able to keep each scrollView's delegate in its own file, even though both files are part of the same class.This ViewController class handles two different sets of tasks, so maybe I should just split them into two different view controllers.
mahboudz
You can create two delegate objects and assign each one to a different scroll view. Splitting the controller itself might be a bad idea as it's very hard to coordinate two controllers for the same view. You might want to create a different classes to handle the scollviews and then make them properties of you view controller so the controller can access them readily.
TechZen
I would add that if the functionality of the scrollview is "dumb" i.e. it is solely related internal functioning of the scrollview independent of any particular data, you could just put the functionality in a scrollview subclass and not have any other object worry about it.
TechZen
Those are two great thoughts. Now, I have never created a standalone delegate. How does one do that without declaring a new class?
mahboudz
You don't. You do create a new class. You create a NSObject subclass that implements the scrollview delegate protocol. The normal practice would be to make those delegates properties of view controller. When the controller loaded the view it would assign the appropriate delegate to each scroll view.
TechZen
+1  A: 

You just need to compare the pointers with what is passed into the methods

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if(scrollView == self.firstScrollView){
          // do something
    }
    else if(scrollView == self.secondScrollView){
          // do something else
    }
}

This is assuming that your scrollviews are properties on the class. I'm pretty sure that a pointer comparison is fine here. If it gives you trouble use something like

if(scrollView.tag == self.firstScrollView.tag)

You will need to assign them a tag when you create them

Joe Cannatti