3

My problem is similar to this one with the only exception - my ImageView appears at the same place inside the window with different content in it. Content has unique identifier which I want to use to call content-specific actions.

To quickly recap, the guy is looking for a way to pass a parameter into the selector section of the initWithTarget method.

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

How can I pass an attribute to the handleTapGesture method or how do I read the unique value otherwise?

Any thoughts appreciated.

EDIT: The content is being pulled from a database and is different every time. The unique identifier is pretty much like an SSN - doesn't repeat.

4

2 回答 2

7

您可以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
    }
}
于 2012-07-03T19:19:30.803 回答
0

尝试扩展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

然后只需将水龙头委托给UIImageViewWithId个人

UIImageViewWithId *imageView = [[UIImageViewWithId ... ]]
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget: imageView  action:@selector(handleTapGesture:)];
于 2015-11-03T00:18:02.737 回答