how to change multiple button image and conditions - ios

I am having two buttons in the table view cell, when I click the 1st button, background image changed to the selected image , at the same time my 2nd button image should not change when 1st button is selected. how can I do this process.
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"l"ofType:#"png"]] forState:UIControlStateNormal];
button1.tag = 1;
[button1 addTarget:self action:#selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(160, 27, 36, 36);
[button2 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"e"ofType:#"png"]] forState:UIControlStateNormal];
button2.tag = 2;
[button2 addTarget:self action:#selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button2];
- (void)radiobtn:(UIButton *)button
{
}
These buttons are in table view cell,1st button selected image name = blue image.
2nd button selected image name = red image. please help me i am new to IOS.

please use different selector and do the code in different function.For now you have used only a single method i.e . radiobtn:(UIButton *){}
Or if you want to keep the same selector then you can place a check of tags which button is clicked and perform the code accordingly
-(void)radiobtn:(UIButton *)button
{
if(button.tag==1)
{
// perform the code when button 1 is clicked and change the images accordingly
}
else
{
// perform the code when button 2 is clicked
}
}

Related

How to select single button on UITableView

I have an issue in an UITableView as I need to place a button on each row of the tableview and if i click the button, it gets selected at a time and selecting is like toggle. How to achieve this, please help me out to solve this problem.
Below is the following code that am trying to get it out. I am creating button on cellForRowAtIndexPath as shown below:
UIButton *Btn = [[UIButton alloc]init];
Btn .frame = CGRectMake(3, 10, 33, 30);
[Btn setImage:[UIImage imageNamed:#"deselect.png"] forState:UIControlStateNormal];
[Btn addTarget:self action:#selector(method:) forControlEvents:UIControlEventTouchUpInside];
Btn.tag=indexPath.row;
[cell.contentView addSubview:Btn];
and the method: for button action:
-(void)method:(UIButton*)sender
{
NSIndexPath *indexpath1 = [NSIndexPath indexPathForRow:sender.tag inSection:0];
Btn = (UIButton *)sender;
//Btn.tag=sender.tag;
[Btn setImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateNormal];
}
You have an array to store whether the button of a cell is selected or not. In the cellForRowAtIndexPath method, you should set 2 images for 2 states UIControlStateNormal and UIControlStateSelected of the button:
[Btn setImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateSelected];
[Btn setImage:[UIImage imageNamed:#"deselect.png"] forState:UIControlStateNormal];
Then in the method: function, you switch the state of the button via Btn.selected based on the value in the array.
UIImage * btnLinkNormal = [UIImage imageNamed:#"select.png"];
[cell.linkButton setImage:btnLinkNormal forState:UIControlStateNormal];
// Add image to button for pressed state
UIImage * btnLinkHighlighted = [UIImage imageNamed:#"deselect.png"];
[cell.linkButton setImage:btnLinkHighlighted forState:UIControlStateHighlighted];

Toggle Button Image

I have multiple of buttons.
They all load in the viewDidLoad. I want it so once one button has been pressed, it changes to the other image.
So Img1 loads via the viewDidload, then once it's been pressed, it goes to Img2
This is my current code which i have tried:
In the viewdidload:
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(367, 117, 97, 25)];
[btn1 setImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(playSound:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
In the ibaction (which i tried, but im guessing)
-(IBAction)ButtonAction
{
if (btn1==0)
{
btn1=1;
[btn1 setImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
}
else
{
[btn1 setImage:[UIImage imageNamed:#"img2.png"] forState:UIControlStateNormal];
btn1=0;
}
}
Thanks for the help
You need to make sure you are hooking up your UIButton to the proper selector. Also, I would use an #property or a global / instance variable to track which image is currently in the UIButton. Here's some code that should do the trick:
//... in #interface
#property (nonatomic, strong) NSString *btn1ImageName;
- (void)viewDidLoad {
[super viewDidLoad];
// ...
self.btn1ImageName = #"img1.png";
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(367, 117, 97, 25)];
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(toggleImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
- (void)toggleImage:(UIButton *)btn1 {
self.btn1ImageName = ([self.btn1ImageName isEqualToString:#"img1.png"]) ? #"img2.png" : #"img1.png";
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
}
if (btn1==0)
I don't get that part.
One easy solution would be to have two different images for a selected and un-selected state.
btn1.selected = YES, will automatically choose the selected button image.
btn1.selected = NO, will automaticall choose the un-selected button image.

Button is supposed to change image after click, but (randomly) changes images of other buttons too

In my cellForRowAtIndex I initialize a button (it gets shown in every cell in the tableview):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f,cell.frame.size.height -15, 160.0f, 15.0f);
UIImage *buttonImage = [UIImage imageNamed:#"Image1.png"];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button.titleLabel setFont:[UIFont fontWithName:#"Arial" size:13.0]];
button.contentEdgeInsets = UIEdgeInsetsMake(1, 60, 0, 0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cell addSubview:button];
button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
It has a normal image and if the user clicks on the button, the image changes. Also I get the contents of the button.title from a XML. Therefore I tell it to get the right title from the web. This works fine, but sometimes (not all the time) and also randomly it changes the button from the next cell, or the cell three rows down. I don't know why and couldn't find anything while running with breakpoints/debugger.
-(void)customActionPressed :(id)sender
{
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
NSIndexPath *pathToCell = [self.tableView indexPathForCell:owningCell];
UIImage *buttonImage = [UIImage imageNamed:#"image2.png"];
[self doXMLParsing];
[sender setBackgroundImage:buttonImage forState:UIControlStateNormal];
NSString *TableText = [[NSString alloc] initWithFormat:#"%#", [[tabelle objectAtIndex:pathToCell.row] pressed1]];
[sender setTitle:TableText forState:UIControlStateNormal];
((UIButton *)sender).enabled = NO;
}
So maybe someone can see where I went wrong. If you need more code, just say so.
[button addTarget:self action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
Try having UIControlEventTouchUpInside instead of UIControlEventTouchDown.
also you create a custom cell and then you can create a property in the class subclassing UITableViewCell for your button and change the image of button through that property.
Also Don't change the image from action of button .You can access the button through cell.yourbuttonName in didSelectRowAtIndexPath delegate of tableview and then change the image.

UIButton doesn't show image

I have a UITableViewCell that I'm adding five UIButtons to. I programmatically create the buttons, add an action, and an image. The buttons are placed in the correct spot, the action works, and the tag is set, but they are clear. The images do not show.
This is what I'm using to create one of the buttons and add them to the cell, this code is in the tableView:cellForRowAtIndexPath: :
if ([[self.compCompletion objectForKey:#"Key" isEqualToString:#"YES"]) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(11, 6, 21, 21);
button.tag = 1;
[button addTarget:self action:#selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"Comp-Completed.png"] forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:button];
}
else {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(11, 6, 21, 21);
button.tag = 1;
[button addTarget:self action:#selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"Comp-Uncompleted.png"] forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:button];
}
I've tried adding: [button setEnabled:YES];, [button setHidden:NO];, and [button setNeedsDislpay]; to the process with no luck. How do I get the button to display the image?
EDIT:
My image exists, the UIImage created, and I can pull the UIImage out of the UIButton and save it to a file. I've gone through each property of the UIButton that controls appearance and changed them. I also tried an empty UIButton of type UIButtonTypeRoundedRect and UIButtonTypeAddContact. I'm now believe that this has something to do with the UITableViewCell not liking UIButtons, or how I'm adding the UIButton to the UITableViewCell.
Make sure that you have checked your project as Target Membership when copying the files. It helped me solve the problem.
My mistake was dragging the images into an images.xcassets file that was part of a pod, NOT the main project. It showed fine in the storyboard, but did not appear in the app.
In case anyone is still looking for an answer: Once the image was in assets.xcassets then I needed to include it in the "copy bundle resources" areas of the Build Phases part of the Target. Then I also had to set a transparent color so that it wasn't obscured.
project->targets->build phases->copy bundle resources-> "+" -> "add other"->select the image in the finder.
Check whether your images exist and are imported. Also it looks to me that besides of the image name the code is identical. A bit cleaner as:
UIImage *buttonImage;
if ([[self.compCompletion objectForKey:#"Key" isEqualToString:#"YES"]) {
buttonImage = [UIImage imageNamed:#"Comp-Completed.png"];
} else {
buttonImage = [UIImage imageNamed:#"Comp-Uncompleted.png"];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(11, 6, 21, 21);
button.tag = 1;
[button addTarget:self action:#selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:button];
Can it definitely find the images? Are they included in the project?
Just to test, try using an image you've already used somewhere else instead of Comp-Completed.png - if that works, you know that the problem is that it's failing to find that file.
UIImage * normalImage = [UIImage imageNamed:#"Comp-Uncompleted.png"];
UIImage * selectedImage = [UIImage imageNamed:#"Comp-Completed.png"];
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
button.frame = CGRectMake(11, 6, 21, 21);
button.tag = 1;
[button addTarget:self action:#selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:normalImage forState:UIControlStateNormal];
[button setImage:selectedImage forState:UIControlStateSelected];
[cell.contentView addSubview:button];
if ([[self.compCompletion objectForKey:#"Key" isEqualToString:#"YES"]) {
button.selected = YES;
} else {
button.selected = NO;
}
I know it is an old post but I came across the same problem and it was due to isHidden and isUserInteractionEnabled set to values to hide the button.
You have to be careful when to set them to YES or NO.
In case anyone is looking for answer, double check if you have copied the image to Assets.xcassets in project directory not in pods directory.
Second I used render as original image in Assets.xcassets image properties

Highlight the UIButtons created Dynamically?

In my application I have created 20 buttons with in a scroll view, now problem is that I was not able to highlight the selected button.
My intention is to show the pressed button with a different look than normal. When another button is pressed the previous one should become normal:
UIButton *Abutton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[Abutton setTag:i-1];
Abutton.frame = CGRectMake(30.0, 0+j, 40.0, 40.0);
[Abutton setTitle:#"" forState:UIControlStateNormal];
Abutton.backgroundColor = [UIColor clearColor];
[Abutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"image1.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[Abutton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"image2.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[Abutton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[Abutton addTarget:self action:#selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:Abutton];
Finally I created the method for Abutton pressed as below:
-(IBAction)buttonpressed:(id)sender{
Abutton.highlighted=YES;
//.....
//.....
}
If do it like this then only the last button created dynamically gets highlighted. That's not exactly what I wanted.
I think you should replace your current code for the button pressed:
-(IBAction)buttonpressed:(id)sender{
UIButton *b = (UIButton *)sender;
b.highlighted = YES;
//.....
//.....
}
In your example you are specifically highlighting "AButton". This code highlights the button being pressed.
Solution 1:
Create an NSSet with references to all the buttons. In your buttonPressed method, call makeObjectsPerformSelector on the NSSet's buttons, setting them to the unhighlighted state.
Solution 2:
Use a UISegmentedControl. That seems like what you should be doing in this case anyway.

Resources