【问题标题】:Handle tap gesture with an argument iphone / ipad处理带有参数 iphone / ipad 的点击手势
【发布时间】:2012-07-03 19:04:55
【问题描述】:

我的问题类似于this one,唯一的例外是 - 我的 ImageView 出现在窗口内的同一位置,其中包含不同的内容。内容具有唯一标识符,我想用它来调用特定于内容的操作。

为了快速回顾一下,这个家伙正在寻找一种将参数传递到 initWithTarget 方法的选择器部分的方法。

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(handleTapGesture:itemSKU:)];

如何将属性传递给 handleTapGesture 方法,否则如何读取唯一值?

任何想法表示赞赏。

编辑:内容是从数据库中提取的,并且每次都不同。唯一标识符很像 SSN - 不会重复。

【问题讨论】:

  • This 正是我想要做的,但似乎没有人知道答案。应该有办法的。

标签: ios uitapgesturerecognizer


【解决方案1】:

您可以使用您的内容标识符设置UIImageView 标签属性,然后从选择器中读取该信息。

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(handleTapGesture:)];

[imageView addGestureRecognizer:tapGesture];
imageView.tag = 0;

然后:

- (void)handleTapGesture:(UITapGestureRecognizer *)sender
{
    if( ((UIImageView *) sender.view).tag == 0 ) // Check the identifier
    {
        // Your code here
    }
}

【讨论】:

  • 很好的建议 - 也节省了很多麻烦。谢谢!
【解决方案2】:

尝试扩展 UIImageView 并添加您需要的任何值(作为属性)和方法。

@interface UIImageViewWithId: UIImageView

@property int imageId;

@end

然后,如果您想变得更加出色,您可能希望将您的行为封装在这个“小部件”的实现中。这将使您的ViewController 保持整洁,并允许您跨多个控制器使用此小部件。

@implementation UIImageViewWithId

@synthesize imageId;

- (void)handleTapGesture:(UIGestureRecognizer *)gesture {
   NSLog("Hey look! It's Id #%d", imageId);
}

@end

然后只需将点击委派给个人UIImageViewWithIds

UIImageViewWithId *imageView = [[UIImageViewWithId ... ]]
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget: imageView  action:@selector(handleTapGesture:)];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 2015-08-15
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多