1
votes

I have a view controller that has three scroll views as subviews. I would like to have the child scrolling views inform the parent view controller whenever the page has changed.

I thought I would do this by setting up a delegation pattern. My child (a subclass of UIScrollView) sets it up in the header file:

@interface TriptychScrollView : UIScrollView <UIScrollViewDelegate> {
    id delegate;
}

- (id)delegate;
- (void)setDelegate:(id)newDelegate;
@end

@protocol RCScrollDidChange
@optional
- (void)scrollView:(TriptychScrollView *)scrollView imageDidChange:(NSNumber *)index;
@end

The source file has the delegate accessor methods:

- (id)delegate {
    return delegate;
}
- (void)setDelegate:(id)newDelegate {
    delegate = newDelegate;
}

Unfortunately, this means my setting the delegate to self call is being ignored:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setDelegate:self];
    }
    return self;
}

.. and therefore the two methods scrollViewDidScroll: and scrollViewDidEndDecelerating: are not getting called! This means I've effectively lost control of my scrolling views.

Am I doing this incorrectly? Or is there a better way for the child subviews to send messages back to their parents?

1
Do the three scroll views all have their own controller instances? Why are you setting "self" to be the delegate? Usually you would have the parent create the child and set itself as the delegate. - JAgostoni

1 Answers

0
votes

I may not understand the whole problem, but I dont see a reason for implementing your own subclasses of UIScrollView, and if you are insistent on doing so, dont shadow it's .delegate property or make sure and call super.

I would approach this by doing the following:

assuming all of these scrollviews are contained by some other view;

UIScrollView *a = ....
a.delegate = self;
a.tag = 1;

UIScrollView *b = ....
b.delegate = self;
b.tag = 2;

UIScrollView *c = ....
c.delegate = self;
c.tag = 3

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
   if (scrollView.tag == 1) 
     //handle a
   else if (scrollView.tag == 2)
     //handle b
   else if (scrollView.tag == 3)
     //handle c
}