can't change image of uibutton after pressing other buttons - ios

i have 2 buttons that keep with the pressed image even when they are released, until the other button is pressed, then the image returns to the normal image my code is this:
- (IBAction)button1_touch:(UIButton *)sender
{
[Button2 setImage:[UIImage imageNamed:#"b2Released.png"] forState:UIControlStateNormal];
[Button1 setImage:[UIImage imageNamed:#"b1Pressed.png"] forState:UIControlStateNormal];
}
- (IBAction)button2_touch:(UIButton *)sender
{
[Button1 setImage:[UIImage imageNamed:#"b1Released.png"] forState:UIControlStateNormal];
[Button2 setImage:[UIImage imageNamed:#"b2Pressed.png"] forState:UIControlStateNormal];
}
the code above works fine when the app starts, the behavior is the expected pressing both buttons, images change correctly, but when i press other button than those two, the "Button2" once it gets the "B2Pressed.png" image, it never returns to its released image, i wrote a nslog in button1_touch and it gets printed as expected, and the Button1 gets its pressed image, but the Button2 is not getting its released image, is like the method setImage of UiButton gets broken when i press other button than those two, other buttons are independent from those two, for me this behavior has absolutely no sense at all, could it be xcode broken? i am working on xcode 7.2
EDIT:
the problem occurs when i press another button only if that button has a action selector assigned to it (UIControlEventTouchUpInside).

You can use selected state of UIButton for this purpose:
- (void)viewDidLoad {
// ...
// the place where you do buttons setup
[button1 setImage:[UIImage imageNamed:#"b1Released"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"b1Pressed"] forState:UIControlStateSelected];
[button2 setImage:[UIImage imageNamed:#"b2Released"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:#"b2Pressed"] forState:UIControlStateSelected];
}
- (IBAction)firstButtonTouch:(UIButton *)sender {
button1.selected = YES;
button2.selected = NO;
}
- (IBAction)secondButtonTouch:(UIButton *)sender {
button1.selected = NO;
button2.selected = YES;
}

Related

Can't overwrite UIButton options programmatically

Fixed. I had to clear the background image of the button in IB.
I have two buttons in my view that should use different images based on if statements. I created the buttons in IB(and set their size,constraints), but I would like to change their appearance in the viewDidLoad. I made a method to handle the variations, but unfortunately it doesn't work. When I run the app I can log the proper NSLog, but nothing change. It seems my setImage: forState: methods can't overwrite the storyboard option. Do anybody has an idea what did I wrong?
- (void)viewDidLoad {
[super viewDidLoad];
[self setButtonLayout];
}
- (void) setButtonLayout {
if ([[PFUser currentUser][#"status"] isEqualToString:#"active"]) {
[self.bttn1 setImage:[UIImage imageNamed:#"01-normal.png"] forState:UIControlStateNormal];
[self.bttn1 setImage:[UIImage imageNamed:#"01-selected.png"] forState:UIControlStateSelected];
[self.bttn2 setImage:[UIImage imageNamed:#"02-normal.png"] forState:UIControlStateNormal];
[self.bttn2 setImage:[UIImage imageNamed:#"02-selected.png"] forState:UIControlStateSelected];
NSLog(#"1. BUTTON");
}
if ([[PFUser currentUser][#"status"] isEqualToString:#"inactive"]) {
[self.bttn3 setImage:[UIImage imageNamed:#"03-normal.png"] forState:UIControlStateNormal];
[self.bttn3 setImage:[UIImage imageNamed:#"03-selected.png"] forState:UIControlStateSelected];
[self.bttn4 setImage:[UIImage imageNamed:#"04-normal.png"] forState:UIControlStateNormal];
[self.bttn4 setImage:[UIImage imageNamed:#"04-selected.png"] forState:UIControlStateSelected];
NSLog(#"2. BUTTON");
}
}
Try moving your initialization from the viewWillLoad method to the viewWillAppear: method.

How to switch back to original button image after delay (obj-c)?

Hi I am trying to make a simple app that includes buttons changing images on click. I would like them to switch back to the original after a delay of 2 - 3 seconds. Im new to objective c and cant figure out how to do it. I've tried variations of the code below. Since there are several buttons I'd need to retain the sender id or image name. Thanks in advance!
- (IBAction)playSound:(id)sender {
UIImage *newImage = [UIImage imageNamed:#"new.jpg"];
[sender setImage:newImage forState:UIControlStateNormal];
[NSThread sleepForTimeInterval:3];
UIImage *origImage = [UIImage imageNamed:#"orig.jpg"];
[sender setImage:origImage forState:UIControlStateNormal];
}
Add two target of your Button with StateHighlighted and StateNormal, such like,,,
[myButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]] forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d-active.png", i]] forState:UIControlStateHighlighted]
[myButton addTarget:self action:#selector(buttonMenuPressed:) forControlEvents:UIControlEventTouchUpInside];
[myButton addTarget:self action:#selector(gotoList:) forControlEvents:UIControlStateHighlighted];
You need not to change button image. Follow this:
Your UIButton will have to images for two states, UIControlStateNormal and UIControlStateSelected in loadView or in viewDidLoad of in xib itself.
[button setImage:[UIImage imageNamed:#"original.jpg"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"new.jpg"] forState:UIControlStateSelected];
when a user will click on button, button will turn to [UIImage imageNamed:#"new.jpg"] itself. Then inside playSound method fire a NSTimer
-(IBAction)playSound:(id)sender
{
[self aTimer];
}
- (void)aTimer
{
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:#selector(changeImage) userInfo:self.view repeats:NO];
}
- (void)changeImage
{
[button setSelected:NO];
}

UI Button Images Sticking iOS 6

I have this method for a twitter button
-(IBAction)tweetThis:(id ) sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"TwitterButtonBlue.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}
else {
[sender setImage:[UIImage imageNamed:#"TwitterButtonBlack.png"] forState:UIControlStateSelected];
[sender setSelected:YES];}
}
in the simulator when i tap the button (which I made in IB as a touch up inside) the button clicks and changes to images but it doesn't go back to the original button image so for instance the blue.png changes to black.png but stays black and doesn't go back to blue. Any way I canf ix this
If I understand correctly, you're just trying to change the button image when the button is tapped. If so, you can simply just configure the button (in viewDidLoad or interface builder) like this:
[button setImage:[UIImage imageNamed:#"TwitterButtonBlue.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"TwitterButtonBlack.png"] forState:UIControlStateSelected];
The UIButton will handle the image switching for you.

Checkbox control for iOS application

Is there any checkbox control in the object library for iOS applications? The nearest thing I see is the switch control, which can take a boolean value.
You can use the selected state of a UIButton, set different images (or also texts) for differents (via code, or Interface Builder) :
[button setImage:[UIImage imageNamed:#"selected.png"]
forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"unselected.png"]
forState:UIControlStateNormal];
And in the touch up inside action :
- (IBAction)buttonTouched:(id)sender
{
button.selected = !button.selected;
// add other logic
}
//define property of button
Declare Unchecked Image
- (void)viewDidLoad
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
Checked Image.
- (IBAction)buttonTapped:(id)sender
{
if (_checkboxButton.selected == YES)
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateSelected];
_checkboxButton.selected = NO;
}
else
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
_checkboxButton.selected = YES;
}
}
This is the code I use for two checkboxes on the screen. (I put them at the bottom of two pictures that are also on the screen. I use the UIControlEventTouchDown to call the checkBoxTapped method. This method talks to my main view controller where I decide if the answer was correct or incorrect. Then the main view controller calls back the this view and tells it to change the blank textbox to either a check:
or an x
// Put the Checkboxes on the screen
UIImage *checkBox = [UIImage imageNamed:#"Checkbox"];
self.checkBoxLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxLeft setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxLeft setFrame:checkBoxFrameLeft];
[self.checkBoxLeft addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxLeft setTitle:#"checkBoxLeft" forState:UIControlStateNormal];
self.checkBoxLeft.contentEdgeInsets = insets;
self.checkBoxLeft.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
self.checkBoxRight = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxRight setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxRight setFrame:checkBoxFrameRight];
[self.checkBoxRight addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxRight setTitle:#"checkBoxRight" forState:UIControlStateNormal];
self.checkBoxRight.contentEdgeInsets = insets;
self.checkBoxRight.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
- (void)checkBoxTapped:(UIButton *)buttonPressed {
[[self delegate] checkBoxTapped:buttonPressed.currentTitle];
}
- (void)highlightCheckbox:(NSString *)checkboxToHighlight withHighlight:(NSString *)highlight {
if ( [checkboxToHighlight isEqualToString:#"left"] ){
[self.checkBoxLeft setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
} else {
[self.checkBoxRight setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
}
}

UIButton - On touch change image

When I touch the button at that time I want to change image & when i release the touch button image is as it is.
I want to apply below code but it's not with my expectation.
please give me any suggestion.....
-(IBAction)actionEnter:(id)sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateNormal];
[sender setSelected:NO];
} else {
[sender setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateSelected];
[sender setSelected:YES];
}
You can use UIControlStateHighlighted for this.
[myButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateHighlighted];
You can also set this from interface builder by setting the image for highlighted state.
I think this should do it. Set the images after creating the button
[yourButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateSelected];
[yourButton setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateNormal];
and do this
- (IBAction)actionEnter:(id)sender{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
}
In Swift:
button.setImage(UIImage(named: "enter.png"), forState: [.Selected, .Highlighted])
I think, you could set the image in the beginning for normal and selected state ..
Try with below when you create the UIButton object. [Use the images as per your requirement]
[myButton setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateSelected];
#7KV7 got me thinking. I have favorite and ignore buttons that I want to use to mark favorite pictures and pictures that I never want to see again. I used his method to initialize the buttons and then slightly modified his method to toggle the buttons on and off.
In this example, if you mark a picture as a favorite, you want to turn off the ignore button and vice versa. The delegate handles the database stuff.
self.favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.ignoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.favoriteButton setImage:[UIImage imageNamed:#"Favorite-Selected"]
forState:UIControlStateSelected];
[self.favoriteButton setImage:[UIImage imageNamed:#"Favorite"]
forState:UIControlStateNormal];
[self.ignoreButton setImage:[UIImage imageNamed:#"Ignore-Selected"]
forState:UIControlStateSelected];
[self.ignoreButton setImage:[UIImage imageNamed:#"Ignore"]
forState:UIControlStateNormal];
If you are just toggling a button on or off, you won’t need to make it a property, since the buttonPressed sender knows which button has been pressed. I need to have them be property since I need to tell the opposite button to turn its highlight off.
- (void)favoriteIgnore:(UIButton *)buttonPressed {
// Toggle the tapped button
buttonPressed.selected = ( buttonPressed.selected) ? NO : YES;
id <ScoringToolbarDelegate> TB_delegate = _delegate;
// Turn off the other button and call the delegate
if ([buttonPressed.currentTitle isEqualToString:#"favorite"]) {
self.ignoreButton.selected = NO;
[TB_delegate favoriteButtonPressed];
} else {
self.favoriteButton.selected = NO;
[TB_delegate ignoreButtonPressed];
}
}
to change the image immediately use the backgroundImage property.

Resources