2012-07-03 46 views
3

我的问题与this one类似,只有一个例外 - 我的ImageView出现在窗口内的同一位置,其中有不同的内容。内容具有唯一的标识符,我想用它来调用特定于内容的操作。用参数处理水龙头手势iphone/ipad

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

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

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

任何想法赞赏。

编辑:内容正在从数据库中拉出,每次都是不同的。唯一标识符与SSN非常相似 - 不重复。

+0

[这](http://stackoverflow.com/questions/6811979/question-about-selectors)正是我想要的要做,但似乎没有人知道答案。应该有办法。 –

回答

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 
    } 
} 
+1

伟大的建议 - 也省了很多麻烦。谢谢! – daspianist

0

尽量延长UIImageView并添加你需要的任何值(属性)和方法。

@interface UIImageViewWithId: UIImageView 

@property int imageId; 

@end 

然后,如果你想变得更棒,你可能想要将你的行为封装在这个“widget”的实现中。这将使您的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:)]; 
相关问题