views:

1210

answers:

2

I have two horizontally scrolling UIScrollViews stacked vertically on top of each other within one ViewController. Each UIScrollView takes up half the screen. I am trying to independently track the position of both UIScrollViews.

I have successfully used this to track the position of the top UIScrollView:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView1 {

int Position = (scrollView1.contentOffset.x);

    if (Position == 0) {
    NSLog(@"Play A"); 
    }

    else if (Position == 280) {
    NSLog(@"Play B"); 
    }

//etc.
}

I would like to track the position of the bottom UIScrollView as well.

When I try to use this:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView2 {

int Position2 = (scrollView2.contentOffset.x);

if (Position2 == 0) {
NSLog(@"Play A2"); 
}

else if (Position == 280) {
NSLog(@"Play B2"); 
}

//etc.
}

I get an error that says "Redefinition of FirstViewConroller scrollViewDidScroll".

So, determined to press on, I tried a rather hackish solution and attempted to use this instead:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView2

{

int Position2 = (scrollView2.contentOffset.x);

if (Position2 == 0) {
NSLog(@"Play A2"); 
}

else if (Position2 == 280) {
NSLog(@"Play B2"); 
}

//etc.
}

The problem with this is that it triggers both methods when I move either of the UIScrollViews. For example -- if I move the bottom UIScrollView 280 pixels (one image) to the right, the output I get in the console is:

PlayB
PlayB2

And if I move the top UIScrollView three images to the right the output I get is:

PlayC
PlayC2

Which doesn't make any sense to me.

I think I may be bumping up against my own incomplete understanding of how delegates work. Within the viewDidLoad method I am setting both:

scrollView1.delegate = self;
scrollView2.delegate = self;

Perhaps this is part of the problem? Maybe I am causing trouble by declaring that both scrollViews have the same delegate? Just not sure.

I've also tried combining all of my conditional statements into one method -- but I only want to track the position of the bottom UIScrollView when it moves and the position of the top UIScrollView when it moves, and putting the logic all in one method reports both positions when either one moves and this is not what I'm looking for.

Any help is appreciated. Would love to solve this particular problem, but would also really love to understand any bigger issues with how I'm approaching this. Still new at this. Keep thinking I'm getting the hang of it and then I run into something that stops me in my tracks..

+2  A: 

This is exactly what the UIScrollView parameter is for. Create connections to scrollView1 and scrollView2 in IB and then do this:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (scrollView1 == scrollView) {
        // Do stuff with scrollView1
    } else if (scrollView2 == scrollView) {
        // Do stuff with scrollView2
    }
}
kperryua
thankyou thankyouthankyouThat was all I needed. So simple.Makes me feel like an idiot. Should have realized I needed to specify which scrollView I wanted to track within the method. Thanks for the push in the right direction, I'm off to the races again.
crgt
A: 

There's no reason why the same object can't be the delegate of several different scrollviews. The reason that -scrollViewDidScroll: passes you the scrollview in question is so that you know which scrollview did scroll.

This is the general pattern for delegate messages.

NSResponder