2012-07-03 47 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:)]; 
相關問題