我有幾個UIImageView,他們每個人都有一個標籤;我有一個圖像陣列,我想要做的是:當用戶點擊其中一個UIImageView時,應用程序將從數組中返回特定的圖像。如何獲取我正在點擊的UIImageView的標籤?
我實現這樣的:
- (void)viewDidLoad
{
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:scroll];
NSInteger i;
for (i=0; i<8; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*100 + i*15, 300, 100)];
imageView.backgroundColor = [UIColor blueColor];
imageView.userInteractionEnabled = YES;
imageView.tag = i;
NSLog(@"%d", imageView.tag);
[scroll addSubview:imageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(findOutTheTag:)];
[imageView addGestureRecognizer:tap];
}
scroll.contentSize = CGSizeMake(320, 115*i);
}
- (void)findOutTheTag:(id)sender
{
// HOW TO FIND THE tag OF THE imageView I'M TAPPING?
}
我想找出imageView.tag
,並通過imageView.tag
到
UIImageView *tappedImage = [imageArray objectAtIndex:imageView.tag];
顯示圖像。
我做了所有標記,問題是我如何才能找出我正在點擊的imageView的tag
?感謝您的閱讀^ _^
這裏的發送者是手勢識別器,所以你實際上必須做'switch((((UIGestureRecognizer *)sender).view.tag)''。 – Anna 2010-12-13 02:49:50
@aBit明顯好起來! – Rog 2010-12-13 02:56:25
+1 - (void)childTapped :(UITapGestureRecognizer *)tapGesture {int tag = tapGesture.view.tag;} //爲我工作 – HDdeveloper 2013-01-13 12:31:58