2012-10-17 5 views
0

UIScrollView에 이미지 묶음을로드하고 크기를 조정하고 각 버튼 위에 위치 시키므로 더 작은 이미지 위에 버튼을 클릭하면 전체 이미지가로드됩니다. 크기 이미지를 새보기로 만듭니다.버튼 매개 변수로 사용자 정의 매개 변수 전달

단추를 클릭 할 때 이미지의 URL을 매개 변수로 전달하고 읽을 때 매개 변수를 콜백 함수에 전달할 수있는 적절한 방법을 찾기 위해 고심하고 있습니다. 여기

내가 지금까지 가지고있는 코드,

for(Card *card in crd){ 

    pocketImage = [self imageWithImage:[UIImage imageNamed:@"pocket.png"] scaledToSize:CGSizeMake(245/2,80/2)]; 
    pocketImageView = [[UIImageView alloc] initWithImage:pocketImage]; 

    cardImage = [self maskImage:[UIImage imageNamed:@"animal.jpg"]]; 
    cardImageView = [[UIImageView alloc] initWithImage:cardImage]; 

    y = (80/2) * inc; 

    if (inc % 2){ 
     x = 125; 
    } else { 
     x = 10; 
     y += (80/2); 
    } 

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [btn addTarget:self action:@selector(testMe) forControlEvents:UIControlEventTouchUpInside]; 
    [btn setFrame:CGRectMake(x,y-40,pocketImageView.frame.size.width,pocketImageView.frame.size.height*2)]; 

    [pocketImageView setFrame:CGRectMake(x,y,245/2,80/2)]; 
    [cardImageView setFrame:CGRectMake(x,y-40,245/2,80/2)]; 

    //add the pocketImageView to the container 
    [imageContainer addSubview:cardImageView]; 
    [imageContainer addSubview:pocketImageView]; 
    [imageContainer addSubview:btn]; 

    inc++; 
} 

콜백 함수,

-(void)testMe { 
    NSLog(@"works"); 
} 

그래서 기본적으로 그 단지의 모든 이미지를 순환 한 다음 UIScrollView에 그들 모두를 추가입니다 , 잘 작동합니다 문제는 내가 알 수있는만큼 불가능한 testMe 메서드에 사용자 지정 매개 변수를 전달하려고 할 때 발생합니다. 그러면 어떻게 단추를 클릭 한 다음 해당 전체 크기를 표시 할 수 있습니까? 새로운 v의 이미지 iew?

답변

0

iVar/(개인) @ 속성을 사용할 수 있습니다.

그냥이 같은 가능한 할 데이터 설정 :

myTransferVariable = // something you need in you testMe method 

을하고 - (void)testMe:(id)sender {로 작업을 정의 할 수 있어야한다이

-(void)testMe { 
    NSLog(@"%@", myTransferVariable); 
} 
0

처럼 사용하고 @selector(testMe:)로 지정을 . 그런 다음 각 단추에 URL 문자열 배열에 대한 오프셋 (예 :)이있는 태그를 제공하면 메서드는 ((UIButton *)sender).tag을 사용하여 선택한 단추와 일치하는 URL을 찾을 수 있어야합니다.