Previous Radio buttons state not cleared objective c - ios

What I want?
Custom radio buttons, allowing a user to select a single option among multiple options
Code
- (void)setupBtn{
self.radioBtn = [UIButton buttonWithType: UIButtonTypeCustom];
[self.radioBtn setFrame:CGRectMake(40,38,18,18)];
[self.radioBtn setImage: [UIImage imageNamed:#"radio-button"]forState:UIControlStateNormal];
[self.radioBtn setImage: [UIImage imageNamed:#"radio-button-select"]forState: UIControlStateSelected];
[self.radioBtn addTarget:self action:#selector(radioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view2 addSubview:self.radioBtn];
}
- (IBAction)radioButtonTapped:(UIButton*)sender{
if (self.radioBtn.isSelected == true) {
[self.radioBtn setSelected:NO];
self.view1.backgroundColor =[UIColor clearColor];
[self.radioBtn setImage:[UIImage imageNamed:#"radio-button"] forState:UIControlStateNormal];
}else {
[self.radioBtn setSelected:YES];
self.view1.backgroundColor = [UACFCustomColors continentalLightBlueTransparent];
[self.radioBtn setImage:[UIImage imageNamed:#"radio-button-select"] forState:UIControlStateSelected];
}
}

Try storing all your buttons in an array. When one gets tapped, loop through the array, and check if each button is selected. If the button is selected, set it to not be, then exit the loop. After the loop, set the button that was tapped (sender) to be selected.
Untested example code:
- (void) viewDidLoad {
self.radioBtn = [UIButton buttonWithType: UIButtonTypeCustom];
[self.radioBtn setFrame:CGRectMake(40,38,20,20)];
[self.radioBtn setImage: [UIImage imageNamed:#"radio-button"]forState:UIControlStateNormal];
[self.radioBtn setImage: [UIImage imageNamed:#"radio-button-select"]forState: UIControlStateSelected];
[self.radioBtn addTarget:self action:#selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.radioBtn setSelected:YES];
[self.view2 addSubview:self.radioBtn];
self.radioBtn2 = [UIButton buttonWithType: UIButtonTypeCustom];
[self.radioBtn2 setFrame:CGRectMake(40,38,20,20)];
[self.radioBtn2 setImage: [UIImage imageNamed:#"radio-button"]forState:UIControlStateNormal];
[self.radioBtn2 setImage: [UIImage imageNamed:#"radio-button-select"]forState: UIControlStateSelected];
[self.radioBtn2 addTarget:self action:#selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.radioBtn2 setSelected:NO];
[self.view2 addSubview:self.radioBtn2];
self.radioBtn3 = [UIButton buttonWithType: UIButtonTypeCustom];
[self.radioBtn3 setFrame:CGRectMake(40,38,20,20)];
[self.radioBtn3 setImage: [UIImage imageNamed:#"radio-button"]forState:UIControlStateNormal];
[self.radioBtn3 setImage: [UIImage imageNamed:#"radio-button-select"]forState: UIControlStateSelected];
[self.radioBtn3 addTarget:self action:#selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.radioBtn3 setSelected:NO];
[self.view2 addSubview:self.radioBtn3];
self.buttonArray = #[self.radioBtn, self.radioBtn2, self.radioBtn3];
}
- (void) myButtonTapped:(UIButton*) sender {
for(UIButton *btn in self.buttonArray) {
// Only one button should be selected at a time, so once we find it,
// set it to not be selected then break from the loop
if(btn.isSelected){
[btn setSelected:NO];
break;
}
}
// set the button that was tapped to be selected
[sender setSelected:YES];
}
To send one button's text to another button's is fairly simple.
[btn2 setTitle:[btn1 titleForState:UIControlStateNormal] forState:UIControlStateNormal];

Related

Set only single option in iOS

I have two custom round rect button and on this did tap event on image show.Like When we select gender there is only one option to select other automatically hide.but my gives when i touch first button than it select but when i touch second button no operation select.
My main moto is that i want to choose gender but in iOS no radio button use so i design custom and on click event set image.
calling api is a string type.
-(void)viewdidload
{
button = [UIButton buttonWithType:UIButtonTypeCustom];
callApi=#"MALE";//calling Api is a string type//
[button addTarget:self action:#selector(roundButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)roundButtonDidTap:(UIButton*)tappedButton
{
if([callApi isEqualToString:#"FEMALE"]) {
UIImage *btnImage = [UIImage imageNamed:#"bullet.png"];
[button setImage:btnImage forState:UIControlStateNormal];
}
if ([callApi isEqualToString:#"MALE"]) {
UIImage *btnImage = [UIImage imageNamed:#"bullet.png"];
[button2 setImage:btnImage forState:UIControlStateSelected];
button.imageView.hidden=YES;
}
}
try this code:
-(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];
[btnFemale setSelected:NO];
}
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];
[btnMale setSelected:NO];
}
else
{

How To reset the original background image of button in Objective-C?

Im currently working on an app made by someone else. I want to change some things to see how it will work. Currently you have one option to choose an answer and cant change it to something different. I want to change that. I want the user to be able to change the answer before submitting it. The problem is I dont know the name or where i can find the original image for the button. Is there a way to just reset to its original?
Currently when you drag and drop the answer button on correct answer button(I know its confusing. in this app you drag a button with an image on answer button and the image changes). When i try to change the answer the image changes also but previous selections also stays selected.
(Sorry dont know how to properly post code in here)
- (void) setDefaultImage
{
// 1) set all the answer button images to unselcted image
[Ans11 setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[Ans12 setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[Ans13 setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[Ans14 setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[Ans21 setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[Ans22 setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[Ans23 setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[Ans24 setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[Ans31 setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[Ans32 setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[Ans33 setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[Ans34 setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
}
- (void) buttonDroppedOnAnAnswer:(UIButton *)droppedButton
{
if(CGRectIntersectsRect(droppedButton.frame, Ans11.frame))
{
[Ans11 setBackgroundImage:[UIImage imageNamed:#"ans2.png"] forState:UIControlStateNormal];
//[Ans11 setImage:nil];
}
else if(CGRectIntersectsRect(droppedButton.frame, Ans12.frame))
{
[Ans12 setBackgroundImage:[UIImage imageNamed:#"ans2.png"] forState:UIControlStateNormal];
//[Ans12 setImage:nil];
}
else if(CGRectIntersectsRect(droppedButton.frame, Ans13.frame))
{
[Ans13 setBackgroundImage:[UIImage imageNamed:#"ans2.png"] forState:UIControlStateNormal];
//[Ans13 setImage:nil];
}
else if(CGRectIntersectsRect(droppedButton.frame, Ans14.frame))
{
[Ans14 setBackgroundImage:[UIImage imageNamed:#"ans2.png"] forState:UIControlStateNormal];
//[Ans14 setImage:nil];
}
[droppedButton setUserInteractionEnabled:YES];
}
//Method working when the end of touch is recognized:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self setDefaultImage];
//Assigning the coordinations of the initial position of but1, but2, but3, but4:
CGPoint position1;
position1.y=226;
position1.x=974;
CGPoint position2;
position2.y=301;
position2.x=974;
CGPoint position3;
position3.y=377;
position3.x=974;
CGPoint position4;
position4.y=458;
position4.x=974;
self.view.multipleTouchEnabled = NO;
//Long list of conditions, what will happen if each but will intersect with each picture or each answer:
//if but1 will be dropped on Ans11...
if (CGRectIntersectsRect(but1.frame, Ans11.frame))
{
[self buttonDroppedOnAnAnswer:but1];
//...the backgound of Ans11 will change for ans2.png image:
[Ans11 setBackgroundImage:[UIImage imageNamed:#"ans2.png"] forState:UIControlStateNormal];
//User interaction is enabled to allow reuse of the button:
but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans12.frame))
{
[self buttonDroppedOnAnAnswer:but1];
[Ans12 setBackgroundImage:[UIImage imageNamed:#"ans2.png"] forState:UIControlStateNormal];
//[Ans11 setImage:nil];
but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans13.frame))
{
[self buttonDroppedOnAnAnswer:but1];
[Ans13 setBackgroundImage:[UIImage imageNamed:#"ans2.png"] forState:UIControlStateNormal];
but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans14.frame))
{
[self buttonDroppedOnAnAnswer:but1];
[Ans14 setBackgroundImage:[UIImage imageNamed:#"ans2.png"] forState:UIControlStateNormal];
but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans21.frame))
{
[Ans21 setBackgroundImage:[UIImage imageNamed:#"ans2.png"] forState:UIControlStateNormal];
but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans22.frame))
{
[Ans22 setBackgroundImage:[UIImage imageNamed:#"ans2.png"] forState:UIControlStateNormal];
but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans23.frame))
{
[Ans23 setBackgroundImage:[UIImage imageNamed:#"ans2.png"] forState:UIControlStateNormal];
but1.userInteractionEnabled=YES;
}
EDIT: I have figured out a different way to do this. Instead i added another draggable button which simply cancel the previous selection. This is a much simpler way but might require more repetitive lines of code. Thank you very much for your help!!
The backgroundImageForState method of UIButton returns the UIImage
- (UIImage *)backgroundImageForState:(UIControlState)state
So pretty much how you described:
UIImage *defaultImage = [UIImage imageNamed:#"default.png"];
// Suppose you have initially set the image of the button as -
UIButton * answerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[answerButton setImage:defaultImage forState:UIControlStateNormal];
// Later to Check
if ([answerButton backgroundImageForState:UIControlStateNormal] == defaultImage) {
// as the answer button background already contains the default image
// we need not to set the image
}
else
{
// as the answer button background contains some other image than the default image so we need to set the default image to button background
UIImage *defaultImage = [UIImage imageNamed:#"default.png"];
[answerButton setImage:defaultImage forState:UIControlStateNormal];
}
Suppose the below method is called when you drag and drop the button to any answer -
- (void) buttonDroppedOnAnAnswer
{
// 1) set all the answer button images to unselcted image
// Suppose we have 4 answer buttons then set
[Ans11 setBackgroundImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
[Ans12 setBackgroundImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
[Ans13 setBackgroundImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
[Ans14 setBackgroundImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
// check the intersection of button on the answer
// set the intersected answer button image to selected image
if(CGRectIntersectsRect(but1.frame, Ans11.frame))
{
[Ans11 setBackgroundImage:[UIImage imageNamed:#"ans1.png"] forState:UIControlStateNormal];
//[Ans11 setImage:nil];
but1.userInteractionEnabled=YES;
}
else if(CGRectIntersectsRect(but1.frame, Ans12.frame))
{
[Ans12 setBackgroundImage:[UIImage imageNamed:#"ans2.png"] forState:UIControlStateNormal];
//[Ans12 setImage:nil];
but1.userInteractionEnabled=YES;
}
else if(CGRectIntersectsRect(but1.frame, Ans13.frame))
{
[Ans13 setBackgroundImage:[UIImage imageNamed:#"ans3.png"] forState:UIControlStateNormal];
//[Ans13 setImage:nil];
but1.userInteractionEnabled=YES;
}
else if(CGRectIntersectsRect(but1.frame, Ans14.frame))
{
[Ans14 setBackgroundImage:[UIImage imageNamed:#"ans4.png"] forState:UIControlStateNormal];
//[Ans14 setImage:nil];
but1.userInteractionEnabled=YES;
}
}
In case if you want to select 4 answers with 4 different buttons then you should first set all the answer buttons to default images (during initialization).
- (void) setDeafultImagesOnAnswerButtons
{
// 1) set all the answer button images to unselcted image
// Suppose we have 4 answer buttons then set
[Ans11 setBackgroundImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
[Ans12 setBackgroundImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
[Ans13 setBackgroundImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
[Ans14 setBackgroundImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
}
and then just check the intersection of the dropped button on the answer and set the corresponding image, as depicted in below function -
// if you have more than one drop button call the function with the corresponding dropped button
- (void) buttonDroppedOnAnAnswer:(UIButton *)droppedButton
{
// check the intersection of button on the answer
// set the intersected answer button image to selected image
if(CGRectIntersectsRect(droppedButton.frame, Ans11.frame))
{
[Ans11 setBackgroundImage:[UIImage imageNamed:#"ans1.png"] forState:UIControlStateNormal];
//[Ans11 setImage:nil];
}
else if(CGRectIntersectsRect(droppedButton.frame, Ans12.frame))
{
[Ans12 setBackgroundImage:[UIImage imageNamed:#"ans2.png"] forState:UIControlStateNormal];
//[Ans12 setImage:nil];
}
else if(CGRectIntersectsRect(droppedButton.frame, Ans13.frame))
{
[Ans13 setBackgroundImage:[UIImage imageNamed:#"ans3.png"] forState:UIControlStateNormal];
//[Ans13 setImage:nil];
}
else if(CGRectIntersectsRect(droppedButton.frame, Ans14.frame))
{
[Ans14 setBackgroundImage:[UIImage imageNamed:#"ans4.png"] forState:UIControlStateNormal];
//[Ans14 setImage:nil];
}
[droppedButton setUserInteractionEnabled:YES];
}
As per your requirements, I am posting the code -
Let me summarize the requirements first - (please update me if I am wrong somewhere)
There is a view where you need to show a question to a user which will have multiple choices and user can select multiple options from those choices.
The choices are shown up with the buttons, also there are some buttons which are used to select the choices.
The buttons which are used to select the answer buttons are dragged and dropped on the respective answer buttons.
Initially all the answer buttons will be set to default images, and the also the drag-able buttons will be set to their respective default images.
When any drag-able button is dropped on any answer button than the answer button should be set to some selected image, if any other drag-able button is dropped on some other answer button than the corresponding answer button will also be set to some selected image and so on all the answer buttons will be set to selected image as the buttons are dropped on them.
Also if user taps on a selected answer button image than the image of that answer button will be set to default image.
For instance, I consider there are -
1) 4 drag-able buttons
2) 12 answer buttons
The 4 drag-able buttons will be set to there respective default images, lets say dragButImage1.png, dragButImage2.png, dragButImage3.png, dragButImage4.png. Also we will add the tag value 1, 2, 3, 4 to these buttons.
Also, the 12 answer buttons will be set to there respective default images, lets say ans1.png, ans2.png, ans3.png, ans4.png, ans5.png, ans6.png, ans7.png, ans8.png, ans9.png, ans10.png, ans11.png, ans12.png. And the tag value for these buttons will be 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.
Now the coding part
- (void) initializeView
{
// initialize drag-able buttons to default images
[button1 setTag:1];
[button1 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"dragButImage%d.png", button1.tag]] forState:UIControlStateNormal];
[button2 setTag:2];
[button2 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"dragButImage%d.png", button2.tag]] forState:UIControlStateNormal];
[button3 setTag:3];
[button3 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"dragButImage%d.png", button3.tag]] forState:UIControlStateNormal];
[button4 setTag:4];
[button4 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"dragButImage%d.png", button4.tag]] forState:UIControlStateNormal];
// initialize anser buttons to default images
[ans1 setTag:1];
[ans1 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ans%d.png", ans1.tag]] forState:UIControlStateNormal];
[ans1 addTarget:self action:#selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
[ans2 setTag:2];
[ans2 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ans%d.png", ans2.tag]] forState:UIControlStateNormal];
[ans2 addTarget:self action:#selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
[ans3 setTag:3];
[ans3 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ans%d.png", ans3.tag]] forState:UIControlStateNormal];
[ans3 addTarget:self action:#selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
[ans4 setTag:4];
[ans4 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ans%d.png", ans4.tag]] forState:UIControlStateNormal];
[ans4 addTarget:self action:#selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
[ans5 setTag:5];
[ans5 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ans%d.png", ans5.tag]] forState:UIControlStateNormal];
[ans5 addTarget:self action:#selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
[ans6 setTag:6];
[ans6 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ans%d.png", ans6.tag]] forState:UIControlStateNormal];
[ans6 addTarget:self action:#selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
[ans7 setTag:7];
[ans7 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ans%d.png", ans7.tag]] forState:UIControlStateNormal];
[ans7 addTarget:self action:#selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
[ans8 setTag:8];
[ans8 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ans%d.png", ans8.tag]] forState:UIControlStateNormal];
[ans8 addTarget:self action:#selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
[ans9 setTag:9];
[ans9 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ans%d.png", ans9.tag]] forState:UIControlStateNormal];
[ans9 addTarget:self action:#selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
[ans10 setTag:10];
[ans10 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ans%d.png", ans10.tag]] forState:UIControlStateNormal];
[ans10 addTarget:self action:#selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
[ans11 setTag:11];
[ans11 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ans%d.png", ans11.tag]]forState:UIControlStateNormal];
[ans11 addTarget:self action:#selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
[ans12 setTag:12];
[ans12 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ans%d.png", ans12.tag]] forState:UIControlStateNormal];
[ans12 addTarget:self action:#selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
}
// the below function will be invoked when a drag-able button is dropped on an answer
- (void) aButton:(UIButton *)droppedButton droppedOnAnswer:(UIButton *)answerButton
{
// set the answer button image to selected image
[answerButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ansSelectedImage%d.png", answerButton.tag]] forState:UIControlStateNormal];
// you could also set the drag-able button image to selected image same as above
[droppedButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"droppedButtonSelectedImage%d.png", droppedButton.tag]] forState:UIControlStateNormal];
}
// called when any answer button is touched
// this method will set the default image of the touched answer button
-(void) setMyDefaultImage:(UIButton*)answerButton
{
[answerButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"ans%d.png", answerButton.tag]] forState:UIControlStateNormal];
}

I want to change the UIButton Highlight Image, but without success

I have a UIbutton in my view
I want to change the UIButton Highlight Image, but without success
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 66, 29)];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_normal.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_selected.png"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
- (void)filterAction:(UIButton *)button
{
if(button.isSelected == YES)
{
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateHighlighted];
button.selected = NO;
}
else
{
#warning I want to change the UIButton Highlight Image, but without success
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_normal.png"] forState:UIControlStateHighlighted];
button.selected = YES;
}
}
If you want your button to behave as follows,
normal - filter_button_normal
highlighted (on touch press down on button) - filter_button__down_selected
selected (after removing touch from button) - filter_button_selected;
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_normal.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_selected.png"] forState:UIControlStateHighlighted];
so when the user touches it , image changes to highlighted and when he removes his finger over it, it changes to selected
And the target you added is UIControlEventTouchUpInside
[button addTarget:self action:#selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
so filterAction is called after the user removes his finger from it.
- (void)filterAction:(UIButton *)button
{
// the image will change automatically
if(button.isSelected == YES)
{
button.selected = NO;
}
else
{
button.selected = YES;
}
}

Set permanent button image after tap

i've programmatically created a button with an image for normalState.
But I want to set a new button image when the button is pressed (the new image should be displayed for the rest of the time). I tried something, but it only works during tapping. So the button shows its old image after tapping again.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
[cell addSubview:button];
Thanks for your help.
Try to use this it will helps you.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"image2png"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
// ----------- Add this line in your code -------
[button addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
- (void)btnPressed:(UIButton *)sender
{
[sender setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateNormal];
}
You have to do the setting in the target of the button. Add the following code:
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
and set the image in that method for UIControlStateNormal:
- (void)buttonTapped:(UIButton *)sender {
[button setBackgroundImage:[UIImage imageNamed:#"image2png"] forState:UIControlStateNormal];
}

UIButton won't change picture when HighLighted, will only set picture to grey

I want the picture to turn yellow the second the user touches it. Right now on first tap the image turns a bit grey and on release turns yellow.
Second tap the picture would go back to red instantly. How can i fix this?
-(void)bMethod:(UIButton*)sender
{
sender.selected = !sender.selected;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"red.png"];
UIImage *buttonImageSelected = [UIImage imageNamed:#"yellow.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:#selector(bMethod:) forControlEvents:UIControlEventTouchDown];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImageSelected forState:UIControlStateSelected];
[myButton setImage:buttonImageSelected forState:UIControlStateHighlighted];
[self.view addSubview:myButton];
I ran your code and it's working fine mate. Just double check the image names!
EDIT 1:
replace your code with the followings:
-(void)bMethod:(UIButton*)sender
{
if (sender.imageView.image != [UIImage imageNamed:#"red.png"]){
[sender setImage:[UIImage imageNamed:#"yellow.png"] forState:UIControlStateHighlighted];
[sender setImage:[UIImage imageNamed:#"yellow.png"] forState:UIControlStateNormal];
}
else{
[sender setImage:[UIImage imageNamed:#"red.png"] forState:UIControlStateHighlighted];
[sender setImage:[UIImage imageNamed:#"red.png"] forState:UIControlStateNormal];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"red.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:#selector(bMethod:) forControlEvents:UIControlEventTouchDown];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:myButton];
}
EDIT 2:
-(void)bMethod:(UIButton*)sender
{
sender.selected = !sender.selected;
sender.highlighted = !sender.highlighted;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"red.png"];
UIImage *buttonImageSelected = [UIImage imageNamed:#"yellow.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:#selector(bMethod:) forControlEvents:UIControlEventTouchUpInside];
myButton.adjustsImageWhenHighlighted = NO;
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImageSelected forState:UIControlStateSelected];
[myButton setImage:buttonImageSelected forState:UIControlStateHighlighted];
[self.view addSubview:myButton];
}

Resources