Specific action for each buttons in UIScrollView - objective-c

I have an UIScrollView page . On this UIscrollView I have different images inside buttons you can see it in below code, and I want to load this images in UIWebView when I press the buttons, My question is how can I know which button are click. Would you please give me some hint for implementing this?
I'm new to objective-C
Here is my action: I want to have one action for each images, rightnow I have just one action for all images, I one to have one for each image
- (IBAction)openAction:(id)sender {
NSLog(#"test");
}

The actual button is passed to the IBAction. Setting a tag on the button is one way of doing it. If you created outlets for all your buttons in Interface Builder, you can simply do something like this:
- (IBAction)openAction:(id)sender {
UIButton *b = (UIButton *)sender;
if ([b isEqual:self.outletButton1]) {
// Do something with button 1
}
else if ([b isEqual:self.outletButton2]) {
// Do something with button 2
}
}

Don't use Same Tag for Button. Use Different Tag for different button.
For ex [btn setTag:1] .
you can use [btn setTag:i];
Dynamic value

Give each button a unique tag, in your code or in Interface Builder.
Then in your action you can do:
- (IBAction)openAction:(id)sender {
UIButton *b = (UIButton *)sender;
NSLog(#"button %d is pressed", b.tag);
}
I see that you already give your imageviews tags. Those are not buttons! You should instead create UIButtons whose content are images. See UIButton's setImage:forState:
Edit: In response to your question below, here's an example:
NSMutableArray *bArray = [NSMutableArray arrayWithCapacity:kNumImages];
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", i];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btn setImage:imageName forState:UIControlStateNormal];
btn.tag = 100+i;
[bArray addObject:btn];
}

You don't have to add UIButton over UIImageView. Instead, you can set the image for UIButton like this.
[btn setImage:[UIImage imageNamed:#"image1"] forState:UIControlStateNormal];
You can access the image in the action like this:
- (IBAction)openAction:(id)sender {
if([[btn currentImage] isEqual:[UIImage imageNamed:#"image1"]]){
}
NSLog(#"test");
}

Related

Show touch when clicked UIButton Objective-C

I have a UIButton that has only an UIImage set, when Clicked (long press) the images changes automatically with a shadow, What I want is that even if the button gets clicked but not with a long press the images should have the shadow. I already tried from IB to set the UIButton properties Reverse on Highlight and Shows Touch On Highlight but with no results
This is about UIControl Class. It's default to be with shadow. So if you wanna customize it, you should addTarget to your button to every action on UIButton like tapping,long press,touchUpInside and I think it s not about long press it's just you can't realize the difference on tapping.
in one of my project I customize my button actions like this:
buyButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width*3/4-2, 0, self.frame.size.width/4+4, self.frame.size.height)];
[buyButton addTarget:self action:#selector(buyButtonClicked:) forControlEvents:UIControlEventTouchDown];
[buyButton addTarget:self action:#selector(buyButtonNormal:) forControlEvents:UIControlEventTouchUpInside];
[buyButton addTarget:self action:#selector(buyButtonNormal:) forControlEvents:UIControlEventTouchUpOutside];
and methods of this like:
- (void) buyButtonClicked:(UIButton *) sender {
if ([self.backgroundColor isEqual:[Util UIColorForHexColor:#"fede32"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"fdca2e"];
}
else if([self.backgroundColor isEqual:[Util UIColorForHexColor:#"cfd3dc"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"c0c5d0"];
}
}
- (void) buyButtonNormal:(UIButton *) sender {
if ([self.backgroundColor isEqual:[Util UIColorForHexColor:#"fdca2e"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"fede32"];
}
else if([self.backgroundColor isEqual:[Util UIColorForHexColor:#"c0c5d0"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"cfd3dc"];
}
[delegate purchaseOffer:selectedOffer];
}
If you need more information about UIButton actions there are a lot of question on this site.

Pass custom parameters to button action method

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:
myTransferVariable = // something you need in you testMe method
and use it like this
-(void)testMe {
NSLog(#"%#", myTransferVariable);
}
You should be able to define your action as - (void)testMe:(id)sender { and specify it as #selector(testMe:). If you then give each button a tag that is (for example) an offset into an array of URL strings, the method should be able to find a URL that matches the selected button by using ((UIButton *)sender).tag.

how to modify the properties of a button knowing its name and tag? (objective c)

What would be the code to modify the properties of a button (that was created programmatically) knowing its name and tag. (the amount of buttons created isn't always the same, that's why i assigned a tag to the buttons created)
You could use viewWithTag:
UIButton *btn = (UIButton*)[self.view viewWithTag:1];
//then change the properties
[btn setTitle:#"Press Me" forState:UIControlState];
//etc etc
The advantage of this is that if you have multiple buttons with the same changes you can easily loop through the different buttons
for (int i=0; i<numberOfButtons; i++) {
UIButton *btn = (UIButton*)[self.view viewWithTag:i+1];
[btn setTitle:#"Press Me" forState:UIControlState];
//etc etc
}
This assumes your tags start from 1 and increment.
All you really need to identify a button is its tag. The code would look something like:
if (button.tag == 2) {
button.titleLabel.text = #"New text";
button.enabled = NO;
// etc...
}
Merely place that into whatever function you want to change your button(s) in.

How change the current image's button on Click?

In my file .h :
-(IBAction)Boutton:(id)sender;
In my file .m :
-(IBAction)Boutton:(id)sender
{
UIImage *btnImage1 = [UIImage imageNamed:#"x.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
}
With this code i can change the image of my clicked button (sender).
The question is, how can I change the images of the others buttons (not the sender one) ?
For example if (sender.tag == 4) i would like To have something like :
-(IBAction)Boutton:(id)sender
{
UIImage *btnImage1 = [UIImage imageNamed:#"x.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
UIImage *btnImage2 = [UIImage imageNamed:#"Y.png"];
[Boutton:(1) setImage:btnImage2 forState:UIControlStateNormal];
[Boutton:(2) setImage:btnImage2 forState:UIControlStateNormal];
[Boutton:(3) setImage:btnImage2 forState:UIControlStateNormal];
}
Simply link the other buttons to outlets of your UIViewController subclass. So that from -(IBAction)Boutton:(id)sender you can change their image by accessing them through their properties.
The question is, how can I change the images of the others buttons (not the sender one) ?
Use tag parameters of UIButton.
UIButton *btn = (UIButton *)sender;
if(btn.tag == 1)
{
...
}
choose the button and assign the image you want then from the utilities bar change the state config to Highlighted then assign the on click image
see the screen shot below
screen shot

UIButton created runtime in a loop, now I need a specific function depending on the button touched

I am creating some buttons and putting them inside a UIScrollView like this:
int i = 0;
while (i != numberOfButtons ) {
int updatetY = 160*i;
CGRect buttonFrame = CGRectMake(updatetY, 0, 160, 60);
UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
UIImage *buttonImage = [UIImage imageNamed:#"bg.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setTitle:[buttonsArray objectAtIndex:i] forState:UIControlStateNormal];
[button addTarget:self action:#selector(MYPROBLEM) forControlEvents:UIControlEventTouchUpInside];
[menuScrollView addSubview:button];
i++;
}
Now I need to build a method to capture which button is being touched, and run a specific bunch of code (see (MYPROBLEM).
Anybody knows what I can do to "trace" which button is being touched and then run a specific function?
something like:
-(void) buttonfunction
{
case ...
doThis
case...
doThat
}
thanks in advance.
You need to use the tag property of the uibutton control. It's a numeric id for the button. You can set a tag on the button based on the i variable in your loop, for instance:
button.tag=i;
Then in your button's action message, you would simply add the id of the button as a parameter and check its tag like so:
-(void) buttonFunction:(id)sender {
switch (sender.tag)
{
//code goes here
}
}
Hope this helps.