Radio buttons selection image in ios - objective-c

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);
}
}

Related

UICollectionViewCell with UIButton radio button - one radioButton selected at time

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

Button click not registering

I have an Xcode 5 button problem.
I create a number of button programatically on a UIViewController and then want to register clicks on these buttons. I use NSLog to track the clicks but it seems that no clicks are registered in the log.
Can anyone help please?
The ultimate aim to to segue to next UIViewController on each button click, but was just testing to see if button clicks were registered first.
Here is how I create the buttons in viewDidLoad (only 1 shown).
NOTE: the if-statement used only to check which buttons were pressed in previous view.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *myString = [NSString stringWithFormat:#"%d", self.tagNumber ];
if (self.tagNumber == 1)
{
hotelButton = [UIButton buttonWithType:UIButtonTypeCustom];
hotelButton.tag = 1;
hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f);
UIImage *buttonImage1 = [UIImage imageNamed:#"buttonImage1.png"];
[hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
[self.view addSubview:hotelButton];
// Add targets and actions
[hotelButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside];
}
}
And here is how I check for button clicks
- (void) buttonClicked
{
NSLog(#"Button %i clicked", hotelButton.tag);
}
NOTE: I also tried the following:
- (void) buttonClicked:(UIButton *) button
{
NSLog(#"Button %ld clicked", (long int)[button tag]);
}
But that gave an error "undeclared selector buttonClicked" in my viewDidLoad pointing to action#selector):
[hotelButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside];
Use this:
[hotelButton addTarget:self action:#selector(buttonClicked)
forControlEvents:UIControlEventTouchUpInside];
From Apple Docs:
UIControlEventTouchUpInside
A touch-up event in the control where the finger is inside the bounds of the control.
UIControlEventTouchUpOutside
A touch-up event in the control where the finger is outside the bounds of the control
And keep this method:
- (void) buttonClicked
{
NSLog(#"Button %i clicked", hotelButton.tag);
}
try and change outside to inside,
[hotelButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
Try this,
[hotelButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
and pass the sender,
- (void) buttonClicked:(UIButton*)sender
{
NSLog(#"Button %i clicked", [sender tag]);
}
Have modified your if condition. Please follow :-
if (self.tagNumber == 1)
{
hotelButton = [UIButton buttonWithType:UIButtonTypeCustom];
hotelButton.tag = 1;
hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f);
UIImage *buttonImage1 = [UIImage imageNamed:#"buttonImage1.png"];
[hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
hotelButton.showsTouchWhenHighlighted = YES;
hotelButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
[hotelButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:hotelButton];
}

How to hide button after the button is manual added in Cocoa Touch?

I have added button manually in for loop, after that, how to hide or change the button and hide label?
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 170, 150.0, 30.0);
[button setTitle:#"My Button" forState:UIControlStateNormal];
[button addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UILabel *lblFileName = [[UILabel alloc] init];
lblFileName.text = [[objectArray objectAtIndex:i] valueForKey:#"fileName"];
lblFileName.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:lblFileName];
-(IBAction)myAction:(id)sender
{
// hide the button or change title button and hide label
}
If u are adding buttons & labels in for loop, then try following solution.
button.tag = i + 1;//0 is default tag for all views.
lblFileName.tag = i + 1;
-(IBAction)myAction:(id)sender
{
UIButton *btn= (UIButton *)sender;
[btn setHidden:YES];// hide the button
btn.titleLabel.text = [[objectArray objectAtIndex:[sender tag]] valueForKey:#"fileName"];
//Get the label of selected button using button tag.
[[self.view viewWithTag:[sender tag]] setHidden:YES];
}
-(IBAction)myAction:(id)sender
{
// hide the button or change title button
[button setHidden:YES];
}
In myAction:(id)sender sender is your Button.
- (IBAction)myAction:(id)sender {
[sender setHidden:YES];
}
But in this case you cannot make the button visible anymore, because you have no reference to it. I think it would be better to create a property for the button.
-(IBAction)btn:(id)sender
{
btn.hidden=YES;
}
try this.
Other answerers have already shown how sender is the button you want, butif you want to access the button in an different method (for example, if you want to make it visible again) you will need to create a property on the object that created the button.
To do so, put this in your class #interface:
#property (strong) UIButton *myButton;
And #synthesize it in your #implementation:
#implementation WhateverClass
#synthesize myButton = _myButton;
Then you can set the property to your custom button:
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.myButton.frame = CGRectMake(80.0, 170, 150.0, 30.0);
[self.myButton setTitle:#"My Button" forState:UIControlStateNormal];
[self.myButton addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.myButton];
and access it anywhere (after it has been created) like so:
-(IBAction)myAction:(id)sender
{
[self.myButton setTitle:#"Pressed!" forState:UIControlStateNormal];
[self.myButton setHidden:YES];
}
Give unique tag for each button and label like Girish said.No two tags should not be same.
Consider you have 10 button and 10 label. Then give tag for button 1 to 10 and label 11 to 20.
Then on your method you can access any label or button using corresponding tag.
UIButton *button=(UIButton *)[self.view viewWithTag:1];
UILabel *label=(UILabel *)[self.view viewWithTag:11];

Special tag for UIButton?

I have 3 different UIButtons for each image grouped. I have IDs for each image. Right now, I have a special id for each image, and I set the button with that tag.
I want to change the background image of the one selected when you tap it. The problem is, is that 3 buttons have the same tag so I cannot change the right button's background image.
Here's what I have:
UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[likeButton setBackgroundColor:[UIColor clearColor]];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button.png"] forState:UIControlStateNormal];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateSelected];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateHighlighted];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateDisabled];
[likeButton setFrame:CGRectMake(13, 52 + (285 * count), 51, 55)];
[likeButton addTarget:self action:#selector(likeDudle:) forControlEvents:UIControlEventTouchDown];
[likeButton setTag:theIdInt];
[likeButton setTitle:#"no_like" forState:UIControlStateNormal];
[scrollView addSubview:likeButton];
- (IBAction)likeDudle: (id)sender {
NSInteger tagId = ((UIControl*)sender).tag;
UIButton *tempButton = (UIButton*)[scrollView viewWithTag:tagId];
NSLog(#"likeDudle: %d // %#", tagId, tempButton.titleLabel.text);
if ([tempButton.titleLabel.text isEqualToString:#"no_like"]) {
[tempButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateNormal];
[tempButton setTitle:#"like" forState:UIControlStateNormal];
} else if ([tempButton.titleLabel.text isEqualToString:#"like"]) {
[tempButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button.png"] forState:UIControlStateNormal];
[tempButton setTitle:#"no_like" forState:UIControlStateNormal];
}
Is there a better way in doing this?
Thanks,
Coulton
If you have fewer than 10 images, then give the ith button the tag i*10+image.tag. Then you can retrieve the image.tag by button.tag % 10 and the button tags will be unique. You can even retrieve the button only information by int b = button.tag/10.
Also, you can access the button's image's tag with button.backgroundImage.tag, so then the button could have its own separate tagging system, depending on your uses.

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?