I've got a ViewController which contains a vertical UIScrollView. To said UIScrollView I add multiple subviews, each of which contains a horizontal UIScrollView which contains some images and is paged.
When creating these subviews, I add a UITapGestureRecognizer to their UIScrollViews to detect single taps on them and perform an action depending on which image was being displayed.
The problem I've got is that if I have 2 such subviews and tap the first one in line, it picks up the wrong subview and tries to fire the method on the second one. So, say I tapped the 3rd image of the 1st subview, and the 2nd subview only has 1 page, then the app crashes.
Here's the constructor for my subview:
- (id)init {
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"GalleryLeafView"
owner:nil
options:nil];
GalleryLeafView *galleryLeafView = [arrayOfViews objectAtIndex:0];
self = galleryLeafView;
if (self) {
// Set up tap gesture recognizers on the ScrollView
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[self.scrollView addGestureRecognizer:tapGestureRecognizer];
}
return self;
}
Here's the selector method, just in case:
- (void)imageTapped {
NSString *filename = [imageFilenames objectAtIndex:[self getCurrentPage]];
NSString *caption = [captions objectAtIndex:[self getCurrentPage]];
[self.galleryLeafViewDelegate galleryLeafViewPressedWithFilename:filename caption:caption];
}
Any ideas as to why this happens? Or how I can work around this?