UICollectionViewCell with UIButton radio button - one radioButton selected at time - objective-c

I have UICollectionView with UIButton as a radio Button in UICollectionViewCell.xib.
I need one radioButton selected at time, if next was pressed, previous should be deselected. I will appreciate help.
//Loading radio button when view loads
-(void)setRadioButton{
if (!self.product.isSelectedCell) {
[self.radioBtn setBackgroundImage:[UIImage imageNamed:#"radio-button"] forState:UIControlStateNormal];
}
else
{
[self.radioBtn setBackgroundImage:[UIImage imageNamed:#"radio-button-select"] forState:UIControlStateNormal];
}
}
//Action for radio button
- (IBAction)radioBtnAction:(UIButton*)sender {
if([self.radioBtn isSelected]==YES)
{
[self.radioBtn setBackgroundImage:[UIImage imageNamed:#"radio-button-select"] forState:UIControlStateNormal];
}
else{
//always calls else
[self.radioBtn setBackgroundImage:[UIImage imageNamed:#"radio-button"] forState:UIControlStateNormal];
}
}

Use this code in cellForRowAtIndexPath
UIButton *radioButton = (UIButton *) [cell.contentView viewWithTag:kRadioButtonTag]; // Radio button
[radioButton setImage:[UIImage imageNamed:#"radio_unselected"] forState:UIControlStateNormal];
[radioButton setImage:[UIImage imageNamed:#"RadioSelected"] forState:UIControlStateSelected];
[radioButton addTarget:self action:#selector(radioButtonPressed:) forControlEvents:UIControlEventTouchDown];
if(indexPath == selectedIndexPath)
[radioButton setImage:[UIImage imageNamed:#"RadioSelected"] forState:UIControlStateSelected];
else
[radioButton setImage:[UIImage imageNamed:#"radio_unselected"] forState:UIControlStateNormal];
Radio button taped from table cell and get index path of that button
Take on class Lebel NSIndexPath variable and store selected index path.
- (void)radioButtonPressed:(UIButton *)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:tableView];
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:buttonPosition];
if (indexPath != nil) {
selectedIndexPath = indexPath
[tableView reloadData];
}
}
Hope this will help

Related

Radio buttons selection image in ios

Hello i am using button as a radio button in my app. App is haveing two button as Male and female once use choose male its image change to green if select again its image shoulg grey. Same is happeningn for female button. This working is good. But when i am selected male then i have slect female then male button chage to grey coz at a time you can select only onw gender.
My problem is when i am selecting male then after selecting female male button image is chaging but if user want to set gender male it need to press two times???
-(IBAction)selectMale:(id) sender{ // MALE BUTTON
UIButton *RadioButton1 = (UIButton*)sender;
[self radiobuttonAction:RadioButton1];
}
-(void)radiobuttonAction:(UIButton *)Button
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected];
gender = #"Male";
[btnFemale setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateNormal];
}
}
-(IBAction)selectFemale:(id) sender{
UIButton *RadioButton1 = (UIButton*)sender;
[self radiobuttonActionFemale:RadioButton1];
}
-(void)radiobuttonActionFemale:(UIButton *)Button
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected];
gender = #"Female";
[btnMale setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateNormal];
}
}
In radiobuttonAction Method, add line at last in if condition:
[btnFemale setSelected:NO];
And In radiobuttonActionFemale method, add line at last in if condition:
[btnMale setSelected:NO];
Try resetting the opposite gender (for example reset btnFemale when btnMale is pressed)radio Button before setting the new gender..
Try this approach. Add all buttons (males and females) to array and in button action handler change button states:
- (void) buttonPressed:(UIButton *)sender
{
for (UIButton *button in self.buttonsArray) {
if([button isKindOfClass:[UIButton class]]){
button.selected = (button.state & sender.state);
}
}

Can't set UIButton.highlighted to NO

I have a UITableViewCell with UIButton in each cell. When the button is pressed I set its state to highlighted like in this answer:
[self performSelector:#selector(doHighlight:) withObject:[cell.subviews objectAtIndex:2] afterDelay:0.0];
Then I do this:
- (void)doHighlight:(UIButton *)sender {
if (sender.highlighted) {
[sender setHighlighted:NO];
} else {
[sender setHighlighted:YES];
}
}
But the button not only is just not highlighted at all, not speaking about the fact that I should be able to UNhighlight it.
Any ideas on what is wrong?
I ended up using UIButton's selected property. It does not require any delay and it works brilliantly with this type of things:
if (!sender.selected) {
[sender setSelected:YES];
[cell addSubview:hiddenButton];
[self performSelector:#selector(doHighlight:) withObject:sender];
} else {
[sender setSelected:NO];
[self performSelector:#selector(doUnHighlight:) withObject:sender];
}
Here is a possible solution you are looking for. I have modified my answer
UIButton *button=[cell.subviews objectAtIndex:2];
//I am adding these five lines to ensure the different
//states of the button to achieve your highlighted state goal.
UIImage *highlightImage = [UIImage imageNamed:#"highlight.png"];//Also used when button is selected
UIImage *normalImage = [UIImage imageNamed:#"normal.png"];
[button setBackgroundImage:normalImage forState:(UIControlStateHighlighted)];
[button setBackgroundImage:highlightImage forState:(UIControlStateSelected)];
[button setBackgroundImage:normalImage forState:UIControlStateNormal];
[self performSelector:#selector(doHighlight:) withObject:[cell.subviews objectAtIndex:2]];
-(IBAction) doHighlight:(id)sender
{
if ([sender isKindOfClass:[UIButton class]])
{
UIButton *btn=(UIButton*)sender;
if (btn.isSelected) {
[btn setSelected:NO];
}
else
{
[btn setSelected:YES];
}
}
}

how to add background color to UIbuttons crested by using for loop based on frame sizes?

I have created 5 buttons using for loop based on incrementing y axis value in CGRectMake. But how can i change the background color of button when i clicked each button?and also title AND having alertview for each button??pls suggest me answer..
//array taken for button titles
NSArray *array1=[NSArray arrayWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
int k=0;
for(int i=50;i<=350;i=i+70)
{
button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(30,i,35,40);
[button addTarget:self action:#selector(buttonclicked:) forControlEvents:UIControlEventTouchUpInside];
if (k<[array1 count])
{
[button setTitle:[array1 objectAtIndex:k] forState:UIControlStateNormal];
}
k++;
[self.view addSubview:button];
}
Try this
-(void) buttonClicked:(id) sender
{
UIButton * button = (UIButton*) sender;
button.backgroundColor = [UIColor yellowcolor];
[button setTitle:#"Hello" forState:UIControlStateNormal];
}
Set the tag for each button and when it's call buttonclicked: is called, then use it's tag rod decide which background color to set it to::
int temptag;
temptag = 0;
for(int i=50;i<=350;i=i+70)
{
button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(30,i,35,40);
[button addTarget:self action:#selector(buttonclicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag = temptag;
if (k<[array1 count])
{
  [button setTitle:[array1 objectAtIndex:k] forState:UIControlStateNormal];
  }
k++;  
temptag++;
[self.view addSubview:button];
}
-(void)buttonclicked:(id)sender
{
UIButton *btn = (UIButton *)sender;
switch (btn.tag) {
case 0:
btn.backgroundColor = [UIColor redColor];
break;
case 1:
btn.backgroundColor = [UIColor grayColor];
break;
case ...:
....
default:
break;
}
}

how to change the button image inside the table cell while clicking the button in iphone

I have a tableview in my application. I created a button inside the table cell and assigned a background image to it. I wanted to change the background image when i click that button. I assigned an action for the button like this
-(void)goodBtnClicked:(id)sender {
[goodBtn setBackgroundImage:[UIImage imageNamed:#"camera.png"] forState:UIControlStateNormal];
}
But, the image is not getting replaced with the new image. Can any one help me with this?
You should make sure that your image is actually there.
-(void)goodBtnClicked:(id)sender {
UIImage *img = [UIImage imageNamed:#"camera.png"];
NSAssert(img, #"Image shouldn't be nil");
[goodBtn setBackgroundImage:img forState:UIControlStateNormal];
}
try with
-(void)goodBtnClicked:(id)sender {
[sender setBackgroundImage:[UIImage imageNamed:#"camera.png"] forState:UIControlStateNormal];
}
Go through all the subviews and locate your button then change background image
- (void)buttonPressed:(UIButton *)sender{
UITableViewCell *cell = (UITableViewCell *)sender.superview;
for (UIView *sub in cell.subviews) {
if([sub isMemberOfClass:[UIButton class]])
{
UIButton * button=[[UIButton alloc] init];
button=sub;
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
}
}
}

Change UIbutton Image on click

I have stored 5 images in an mutable array and displayed them randomly on iPhone view by appending them in UIButton . now I want to change the image on a button on which I will click. but in my code only the last image changes not the image on which I called the action.
Here is the code for 3 buttons which highlights the clicked button
-(void) changeButtonImage:(id) sender{
[button1 setBackgroundImage:[UIImage imageNamed:#"button1Image_off.png"] forState:UIControlStateNormal];
[button2 setBackgroundImage:[UIImage imageNamed:#"button2Image_off.png"] forState:UIControlStateNormal];
[button3 setBackgroundImage:[UIImage imageNamed:#"button3Image_off.png"] forState:UIControlStateNormal];
UIButton *button = sender;
if (button.tag == 0) {
[button1 setBackgroundImage:[UIImage imageNamed:#"button1Image_on.png"] forState:UIControlStateNormal];
}else if (button.tag == 1) {
[button2 setBackgroundImage:[UIImage imageNamed:#"button2Image_on.png"] forState:UIControlStateNormal];
}else if (button.tag == 2) {
[button3 setBackgroundImage:[UIImage imageNamed:#"button3Image_on.png"] forState:UIControlStateNormal];
}
}
hope this helps...
hAPPY cODING...
If you are looking for how to change the image on a button just do:
[myButton setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
You can have it loop through your array but creating a variable to store what image index you are on. Then just go to the next one using the statement above to assign the image.
Do you have an Outlet (IBOutlet) for each of the buttons. You should list each one as an outlet, and just use Interface Builder to connect each button to those variables. Then create a function for the touchUpInside event of each button. Make this buttonPressed. make this function something like:
-(void) buttonPressed:(id) sender
{
((UIButton *)sender).image = [UIImage imageNamed:#"Image.png"];
}
You will want to set a variable like currentImage to track what image is set. Each time you click increase that variable (currentImage++). If it gets > some final amount set it back to 0. Then you can just do
if (currentImage == 0) { set first image; } else if (currentImage == 1) { set second image.. }
etc...
Does this help?