UIButton disable and enable background image - iphone

My application consist of several buttons , when user touch one of them a shadow appears below them, and the rest of buttons should not have any background image , I need something like Instagram application effects , here is my code but my problem is when I touched other button the shadow does not appears .
#define BGB [btn setBackgroundImage:[UIImage imageNamed:#"shadow.png"] forState:UIControlStateNormal]
#define _BGB [btn setBackgroundImage:nil forState:UIControlStateNormal]
- (IBAction)effectsPerform:(id)sender {
UIButton *btn = (UIButton *)sender;
if (btn == EB0) { BGB; } else { _BGB; }
if (btn == EB1) { BGB; } else { _BGB; }
if (btn == EB2) { BGB; } else { _BGB; }
//and other buttons ...
}

Try this way:
-(void)changeButton:(UIButton *)button background:(UIImage *)image
{
[button setBackgroundImage:image forState:UIControlStateNormal];
}
- (IBAction)effectsPerform:(UIButton *)sender
{
[self changeButton:sender background:(sender == EB0 || sender == EB1 || sender == EB2) ? [UIImage imageNamed:#"shadow.png"] : nil];
}
UPDATE:
You do it with another approach, in your viewDidLoad: method, for every button you can set:
[button setBackgroundImage:nil forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"shadow.png"] forState:UIControlStateSelected];
And then in your -(IBAction)effectsPerform:
- (IBAction)effectsPerform:(UIButton *)sender
{
button1.isSelected = (sender == button1);
button2.isSelected = (sender == button2);
....
}
...this way you guarantee you will have only one selected button. What can you do to optimize your code is you can subclass UIButton and in the init method you call the background setter methods, and then all buttons can be of this type (you can mark them in the interface builder by setting their class) - this way you won't need to set the background images for every state for every button.

Related

how to change the background image of button everytime when i clicked on that button

Hi I want to make a checkbox field in for that I am using two images checkbox.png and unchecked.png and i want to replace this image every time when user clicks on button. so my question is how to change the background image of button every time when i clicked on that button.
can I do this for that
if ([_arsa1.currentImage isEqual:#"checkbox.png"])
{
[_arsa1.currentImage isEqual:#"checkbox_checked-1.png"];
}
else
{
[_arsa1.currentImage isEqual:#"checkbox.png"];
}
if yes then why this not works and if no then suggest me some other code
Thanku
- (void)viewDidLoad
{
[btn setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"checkbox_checked-1.png"] forState:UIControlStateSelected];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(IBAction)btn:(id)sender
{
if(btn.selected == TRUE)
{
btn.selected = FALSE;
}
else
{
btn.selected = TRUE;
}
}
set two images for two controll states(UINormalState and
UISelectedState) and simply set selected YES/NO to that button it'l
change automatically.
[btn setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"checkbox_checked-1.png"] forState:UIControlStateNormal];
-(IBAction)btnAction:(UIButton *)sender{
sender.selected = !sender.selected;
}

add two background image for selected and unselected UISegments iphone

I have two segment buttons (1 and 2) I want to add a background image one for selected segment and another image for unselected segment.How can I do that?
Here is what I have so far,should I set the background image here?:
- (void)selectWithSegment:(UIButton *)sender
{
for (UIButton *button in self.segments) {
if (button == sender) {
if (self.staySelected) {
button.backgroundColor = [UIColor colorWithRed:17.0/255.0 green:17.0/255.0 blue:17.0/255.0 alpha:1.0];
[button setBackgroundImage:nil forState:UIControlStateNormal];
}
if ([self.delegate respondsToSelector:#selector(segmentControl:didSelectSegment:)]) {
[self.delegate segmentControl:self didSelectSegment:sender];
}
} else {
[self decorateButton:button];
}
}
}
how can I set image1.png and image2.png for for selected and unselected segments?Can someone help me with the implementation?
I don't know any other way to handle this situation, but i think this can help you:
After making IBOutlet of UISegmentControl set IBAction to valueChange event with below action:
-(IBAction)selectedSegment:(id)sender{
int totalNumberOfSegment = 5;
UISegmentedControl *tmpSig = (UISegmentedControl *)sender;
NSLog(#"Inside sender method");
for (int i = 0; i < totalNumberOfSegment; i++) {
[tmpSig setImage:[UIImage imageNamed:#"unselect.png"] forSegmentAtIndex:i];
}
[tmpSig setImage:[UIImage imageNamed:#"select.png"] forSegmentAtIndex:tmpSig.selectedSegmentIndex];
}

iPhone . button image not switching

I have implemented a radio button on one of my apps, exactly like this solution:
http://www.developers-life.com/radio-buttons-in-iphone-application.html
but my checkboxButton method is like this:
- (IBAction)checkboxButton:(UIButton *)button{
for (UIButton *but in self.arrayButtons) {
if (but == button)
[but setSelected:YES];
else
[but setSelected:NO];
}
}
I have two buttons belonging to the same radio button group. When I select one of the buttons its image toggles to the switched one. Perfect. The problem is that when I select the other one it will not select to selected state and the first one that is being displayed as selected continues to be displayed as selected.
any clues?
for (UIButton *but in [Your_View subviews]) {
if ([but isKindOfClass:[UIButton class]] && ![but isEqual:button]) {
[but setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
why are you using so much complicated just use below to make selected..
- (IBAction)checkboxButton:(UIButton *)button
{
button.selected = !button.selected;
}
and make sure your images for button are set in the xib for each state as required.
In order to achieve the desired functionality, you will have to do selection of buttons in the next run loop.
So, call another method from "checkboxButton:".
Do something like,
- (IBAction)checkboxButton:(UIButton *)button
{
[self performSelector:#selector(highlighButton:) withObject:button afterDelay:0.0];
}
Where "highlightButton:" is defined as:
-(void) highlighButton:(UIButton*)aButton
{
[aButton setHighlighted:YES];
for (UIButton* button in self.arrayButtons) {
if (button != aButton) {
[button setHighlighted:NO];
}
}
}
I just made a few slight tweaks, and I just tested this and it works.
viewDidLoad: //making sure all buttons have the same selector assigned
[button1 addTarget:self action:#selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self action:#selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
[button3 addTarget:self action:#selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
Then just slightly modifying the loop to scan for button subviews instead of objects in an arbitrary array (should be more accurate)
- (IBAction)checkboxButton:(UIButton *)button{
for (UIButton *but in self.view.subviews) {//Change "self.view" to what ever view you are adding these subviews to.
if (but == button){
[but setSelected:YES];
}else{
[but setSelected:NO];
}
}
}
Keep in mind the selected state image/text that you are assigning will not appear until the button is released. When the button is pressed before it is released it is in its highlighted state. This is just a note so you can set this up accordingly.
After trying all solutions given here and have all failed, I tried a new approached and it worked perfectly.
The solution was to change this
- (IBAction)checkboxButton:(UIButton *)button{
for (UIButton *but in self.arrayButtons) {
if (but == button)
[but setSelected:YES];
else
[but setSelected:NO];
}
}
to this
- (IBAction)checkboxButton:(UIButton *)button{
for (UIButton *but in self.arrayButtons) {
if (but == button)
[but setImage:selectedImage forState:UIControlStateNormal];
else
[but setImage:normalImage forState:UIControlStateNormal];
}
}
apparently, changing the state of the button will not change its image. At least did not work for me.

How can i maintain the selected state in button in iPhone

I have created scroll view and sets the buttons are in the scroll view. The Buttons are scrolling horizontally and it works fine. If i clicked the button, i set background image as "Selected State" in button. My problem is how can i changed the selected state in different button, when clicking it and how can i deselected the "selected state" button when clicking the another button.
I have three buttons in the scroll view,
-(IBAction) Button1 : (id) sender
{
// btn1.selected = YES;
[btn1 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateSelected];
}
-(IBAction) Button2 : (id) sender
{
// btn2.selected = YES;
[btn2 setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateSelected];
}
-(IBAction) Button3 : (id) sender
{
// btn3.selected = YES;
[btn3 setImage:[UIImage imageNamed:#"three.png"] forState:UIControlStateSelected];
}
see the below image,(Health, Entertainment and Money Watch are the three buttons)
Image http://www.freeimagehosting.net/uploads/6b3daab12f.png
and
Img http://www.freeimagehosting.net/uploads/b6e0f234dc.png
Note:(Like, Tabbar and Segmented control)
On clicking first button and sets background image in selected state and clicking the second button, then first buttons are to be deselected. So how can i maintain the selected state, till another button is clicked.
Thanks in Advance.
I solved this task in the following way:
init method:
Create number of buttons with defined images for normal and selected state.
Assign tag for each button (for example, for i'th button tag is 1000+i).
Assign IBAction for each button.
action method:
Remove selection from previously selected button (search it by it's tag with [view viewWithTag:] method)
Select sender.
Save sender's tag.
Here's the code:
- (void)init {
....INITIALIZE SCROLLVIEW HERE.....
for ( int i = 0; i < 10; i++ ) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:.....];
[btn setImage:_IMAGE_ forState:UIControlStateNormal];
[btn setImage:_IMAGE2_ forState:UIControlStateSelected];
[btn setTag:i + 1000];
[btn addTarget:self action:#selector(setSelectedButton:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
}
- (IBAction)setSelectedButton:(id)sender {
[self setSelectedButtonByIndex:((UIButton *)sender).tag - 1000];
}
- (void)setSelectedButtonByIndex:(NSInteger)index {
if ( selectedElemId >= 0 ) {
UIButton *btn = (UIButton *)[self viewWithTag:selectedElemId + 1000];
[btn setSelected:NO];
}
UIButton *btn = (UIButton *)[self viewWithTag:index + 1000];
[btn setSelected:YES];
selectedElemId = btn.tag - 1000;
}

Changing image on UIButton when user presses that button on an iPhone

I want to change the image on UIButton, when User presses that button. So, I wrote
btnthumbnail2 setImage:[UIImage imageNamed:#"leaderboard_ov.png"] forState:UIControlStateNormal];
and after that I changed the view.
The image is changing but not display the changed image.I think the controll goes to next view tha's why it is happening. But I want to show the change, How can I do this?? Plz help me for this..
I am using Interface Builder, in which I added a UIButton to the view.
I defined the button selected by default and selected my image for the selected state (Use drop-down list in Inspector window to choose "Selected State Configuration" before selecting the image).
Create an IBAction in the controller and connect the button to that action.
Then see the code below:
-(IBAction) toggleUIButtonImage:(id)sender{
if ([sender isSelected]) {
[sender setImage:unselectedImage forState:UIControlStateNormal];
[sender setSelected:NO];
} else {
[sender setImage:selectedImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
}
This is the simplest way that I am doing it and it works every time
-(void)buttonTouched:(id)sender
{
UIButton *btn = (UIButton *)sender;
if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"icon-Locked.png"]])
{
[btn setImage:[UIImage imageNamed:#"icon-Unlocked.png"] forState:UIControlStateNormal];
// other statements
}
else
{
[btn setImage:[UIImage imageNamed:#"icon-Locked.png"] forState:UIControlStateNormal];
// other statements
}
}
You can use other similar checks for other states such as SELECTED, ENABLED, HIGHLIGHTED.
The code you have there should work fine, but I'm guessing that the button image doesn't appear changed until the next run through the event loop, so you don't see it before the view gets switched out.
It's kind of a hack, but you could try delaying the view change just slightly so that the button will update.
So instead of this:
[btnthumbnail2 setImage:[UIImage imageNamed:#"leaderboard_ov.png"] forState:UIControlStateNormal];
[self showMyOtherView];
Try this:
[btnthumbnail2 setImage:[UIImage imageNamed:#"leaderboard_ov.png"] forState:UIControlStateNormal];
[self performSelector:#selector(showMyOtherView) withObject:self afterDelay:0.01];
To change the image for the selected state you have to call setImage for state UIControlStateSelected. You can set separate images for different states (Normal, Highlighted, Disabled, Selected, etc).
If you want to change the image when the button is clicked, this way, works perfectly:
-(IBAction)buttonClicked: (id)sender
{
UIButton *buttonObj = (UIButton*)sender;
[buttonObj setBackgroundImage:[UIImage imageNamed:#"myImage.png"]
forState:UIControlStateNormal];
}
The simplest way to accomplish this is to simply set the highlighted image in Interface Builder.
Building on Nicsoft's answer.
-(void)viewDidLoad {
[super viewDidLoad];
[self.button setImage:[UIImage imageNamed:#"unselectedImageName"] forState:UIControlStateNormal];
[self.button setImage:[UIImage imageNamed:#"selectedImageName"] forState:UIControlStateSelected];
}
-(IBAction)toggleButton:(UIButton *)sender {
sender.selected = !sender.selected;
// Add logic here
}
I wrote code that does exactly this last night, and I didn't have to resort to any strange delaying tactics. In this particular case, I'm doing it from inside a tableViewCell. This is more code than you asked for, but it shows the whole process I used. As you can see, I chose not to have any special image for the period of time DURING a button touch.
// Button set up, inside cellForRowAtIndexPath:
// imageFrame is just the rect that you want the button to occupy
[self setFavorite:[[UIButton alloc] initWithFrame:imageFrame]];
[[self favorite] addTarget:self action:#selector(toggleFavorite:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:[self favorite]];
[self draw];
// definition of the callback specified above
- (void) toggleFavorite:(id) sender {
if (favoriteState == 0 ){
favoriteState = 1;
} else {
favoriteState = 0;
}
[self draw];
}
// the draw method, to set the images
// favOn and favOff are statically defined at the top of the class
- (void) draw {
if (favoriteState != 0) {
[[self favorite] setBackgroundImage:favOn forState:UIControlStateNormal];
[[self favorite] setBackgroundImage:favOn forState:UIControlStateHighlighted];
} else {
[[self favorite] setBackgroundImage:favOff forState:UIControlStateNormal];
[[self favorite] setBackgroundImage:favOff forState:UIControlStateHighlighted];
}
}
Hate to add to the plethora of answers, but:
Select images in IB for the button's default and selected states.
Connect an IBAction and keep it simple.
#IBAction func buttonTapped(sender: UIButton){sender.selected = !sender.selected}
Done.