I am loading a bunch of images into a UIScrollView, I then resize them and position a button over each, so when I click on the button over the smaller image, it will load up the full size image in a new view.
I am struggling to find a decent way to somehow pass the image’s URL to the click handler as a parameter when the button is clicked, and from what I have read, passing parameters to callback functions is not possible.
Here is the code I have thus far,
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++;
}
Callback function,
-(void)testMe {
NSLog(@"works");
}
So basically that just loops through all of the images, and then appends all of them to a UIScrollView, which works fine, the problem comes in when I try to pass custom parameters to the testMe method, which isn’t possible as far as I know, so how would I be able to click on the button and then display the corresponding full size image in a new view?
You can use an iVar / (private) @property for this.
Just set the data you want to have available like this:
and use it like this