【发布时间】:2015-01-18 15:43:10
【问题描述】:
我有一个包含垂直 UIScrollView 的 ViewController。对于说 UIScrollView,我添加了多个子视图,每个子视图都包含一个水平 UIScrollView,其中包含一些图像并被分页。
在创建这些子视图时,我向它们的 UIScrollViews 添加了一个 UITapGestureRecognizer 以检测对它们的单击并根据正在显示的图像执行操作。
我遇到的问题是,如果我有 2 个这样的子视图并点击第一个子视图,它会选择错误的子视图并尝试在第二个子视图上触发该方法。所以,假设我点击了第一个子视图的第三个图像,而第二个子视图只有 1 个页面,那么应用程序就会崩溃。
这是我的子视图的构造函数:
- (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;
}
这里是选择器方法,以防万一:
- (void)imageTapped {
NSString *filename = [imageFilenames objectAtIndex:[self getCurrentPage]];
NSString *caption = [captions objectAtIndex:[self getCurrentPage]];
[self.galleryLeafViewDelegate galleryLeafViewPressedWithFilename:filename caption:caption];
}
关于为什么会发生这种情况的任何想法?或者我该如何解决这个问题?
【问题讨论】:
标签: ios objective-c uiscrollview uitapgesturerecognizer