2012-07-03 2 views
3

내 문제는 this one과 유사합니다. My ImageView는 다른 내용이있는 창 내부의 같은 위치에 나타납니다. 콘텐츠에는 콘텐츠 별 작업을 호출하는 데 사용하려는 고유 한 식별자가 있습니다.인수 iphone/ipad를 사용하여 탭 제스처를 처리하십시오.

신속하게 요약하자면, 그 사람은 매개 변수를 initWithTarget 메소드의 선택기 섹션에 전달하는 방법을 찾고 있습니다.

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

가 어떻게이 handleTapGesture 방법에 대한 속성을 전달하거나 어떻게 달리 고유 한 값을 읽습니까?

모든 의견을 감사드립니다.

EDIT : 콘텐츠가 데이터베이스에서 가져오고 있으며 매번 다릅니다. 고유 한 식별자는 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 

그런 다음이 위젯의 ​​구현에 비헤이비어를 캡슐화하고 싶다면 더욱 멋진 기능을 원할 수 있습니다. 이렇게하면 ViewController이 깨끗하고 깨끗하게 유지되며 여러 컨트롤러에서이 위젯을 사용할 수 있습니다.

@implementation UIImageViewWithId 

@synthesize imageId; 

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

@end 

그럼 그냥 개인 UIImageViewWithId s의 탭을 위임

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