【问题标题】:Wrong UITapGestureRecognizer being called (in the wrong UIView)调用了错误的 UITapGestureRecognizer(在错误的 UIView 中)
【发布时间】: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


    【解决方案1】:

    好吧,这是我的另一个菜鸟错误;/

    在选择器方法中,您可以看到我正在对 imageFilenamescaptions 数组进行处理。它们应该是强大的属性,而不仅仅是 .m 文件中设置的变量。现在一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多