iPhone: How to Tell Which Scrollview Calls the Delegate Methods?
-
24-09-2019 - |
Question
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?
Solution
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.
OTHER TIPS
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