调用错误的UITapGestureRecognizer(在错误的UIView中)

时间:2014-11-20 17:04:41

标签: ios objective-c uiscrollview uitapgesturerecognizer

我有一个包含垂直UIScrollView的ViewController。对于所说的UIScrollView,我添加了多个子视图,每个子视图都包含一个水平的UIScrollView,它包含一些图像并被分页。

创建这些子视图时,我会在UIScrollViews中添加一个UITapGestureRecognizer,以检测它们上的单击,并根据显示的图像执行操作。

我遇到的问题是,如果我有2个这样的子视图并点击第一个子视图,它会选择错误的子视图并尝试在第二个子视图上触发该方法。所以,比方说我点击了第一个子视图的第3张图片,第二张子视图只有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];
}

关于为什么会发生这种情况的任何想法?或者我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

好的,这是我的另一个新手错误; /

在选择器方法中,您可以看到我在imageFilenamescaptions数组上执行操作。它们应该是强大的属性,而不仅仅是.m文件中设置的变量。现在一切都很好。