UIButton setSelected: NO doesnt show default background image - ios

I have a custom UIButton with 2 background images:
in Default State: default.png
in Selected State: selected.png
set in the Xib File.
This is gets invoked on button touch down:
-(IBAction)numberSelected:(id)sender{
NSLog(#"Button pressed %#",[sender currentTitle]);
UIButton* button = (UIButton*)sender;
button.selected = !button.selected;
[button release];
}
The wrong behavior is this:
I press the button, the background image switches to selected.png (correct), I press it again, and no background image is shown. I press it another time, the app crashes.

I think you forgot to set : UIControlStateSelected adding the image.
[button setImage:[UIImage imageNamed:#"Selected.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
Edit:
Why do you make [button release]; Of course it will crash the second time.

No need to release the button there. Remove
[button release];

[button release];
This statement has to be removed!

Related

UIButton with UIControlEventTouchDown, image won't change to selected until finger lifted

Trying to get my head around this. I have a UIButton with a UIControlEventTouchDown event that calls:
- (IBAction)pressButton:(id)sender {
NSLog(#"button pressed");
UIButton *button = (UIButton *)sender;
button.selected = YES;
}
I am getting the ,message "button pressed" instantly as expected. However the image won't change to the one of selected until I lift my finger. This doesn't make sense as I am setting the button to selected as soon as the button is pressed. I have my off image set in the interface builder for "default" and my on image set under "selected". I have tried also adding an image in for "highlighted" and this also doesn't work. What am I doing wrong here?
You have setup your button clicked event on UIControlEventTouchDown so the event will fire on TouchDown event of the button and you want to set the image on that so you have to set image's UIControlState.
so try to set image like that :
[button setImage:[UIImage imageNamed:#"pressed.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
that might help you.
You can set the highlighted image for your button. In storyboard its like this:
try with this
- (IBAction)pressButton:(id)sender {
NSLog(#"button pressed");
UIButton *button = (UIButton *)sender;
button.selected = YES;
[button setBackgroundImage:[UIImage imageNamed:#"another.png"] forState:UIControlStateNormal];
}

when i click a button in ios app i want the image to change

the title much explains it really in my button
- (IBAction)LikeBtn:(id)sender
when the button is clicked i want ti to swap images from a small grey icon to a green icon
im trying it this way but i cant seem to get it to work
- (IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage :#"liked.png" forState:UIControlStateSelected];
{
Has Any body any ideas how this can be done at the moment the image is selected in the xcode control panel and is named likebtn.png and need it changing to liked.png after clicked
Assuming you have the image properly added to your project, and it's a png:
- (IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"liked"] forState:UIControlStateSelected];
}
PS: Are you sure you wish to set the image for the selected state? If not, use UIControlStateNormal instead.
I think you need to setImage in viewDidLoad when creating button:
[LikeBtn setImage :[UIImage imageNamed:#"liked.png"] forState:UIControlStateSelected];
In addition to what Maddy says about passing a UIImage rather than a string for a selected state, you will also need to declare that the button is now selected. If you change your code to this, it should work for you
-(IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"liked.png"] forState:UIControlStateSelected];
senderButton.selected = YES;
}
You can also put a if (senderButton.selected) condition within the IBAction method to detect and toggle between button states, changing the state from YES to NO.
Hope this helps

UIButton changes states when being pressed

I have an UIButton configured with two states, default and selected, for different text and background, the set up is done on Interface Builder. The weird thing is when the button is in selected state, and the button is pressed, it automatically changes to default state. After the button is released, it changes back to selected state. Is there way to disable roll-back-to-default behavior?
Forgot to mention that, the type is custom.
Update
tried the following code in viewDidLoad
[self.button setTitle:#"default" forState:UIControlStateNormal];
[self.button setTitle:#"selected" forState:UIControlStateSelected];
[self.button setTitle:#"highlighted" forState:UIControlStateHighlighted];
[self.button setSelected:YES];
The button shows default when pressed.
Selected state and Highlighted state are not mutually exclusive. When you press a selected UIButton, it gets into a selected AND highlighted state.
I don't think there's a way to set that in the IB, but you can do this in code:
[button setImage:[UIImage imageNamed:#"some_image"] forState:UIControlStateSelected | UIControlStateHighlighted];

Disabling Animation After The Button Is Pressed

This button is a series of images that animate a glowing effect, when the button is pressed I'd like the animation to stop:
[self.buttonHintdisabled setBackgroundImage: [UIImage animatedImageNamed:#"c" duration:3.0] forState: UIControlStateNormal];
How is this done?
If there is another way to do this in code that could disable it when the button is pressed, please let me know?
I haven't tried this on a UIButton, but I will give you an idea how I would do it:
Often times we set a target with our UIButton:
[self.buttonHintdisabled addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Within buttonPressed:, we would carry out what we would like to do when the button is pressed.
So, with that in mind, in addition to what you would like to do within buttonPressed, you could try:
[self.buttonHintdisabled setBackgroundImage:[UIImage imageNamed:#"c0.png"] forState:UIControlStateSelected];
Once the action within that method is done, set it back using what you have above:
[self.buttonHintdisabled setBackgroundImage:[UIImage animatedImageNamed:#"c" duration:3.0] forState: UIControlStateNormal];
if you want to use selected button state (UIControlStateSelected)
add this line
[self.buttonHintdisabled setBackgroundImage: [UIImage imageNamed:#"selectedImage"] forState: UIControlStateSelected];
and then in its action
- (void)buttonClicked:(id)sender
{
[self.buttonHintdisabled setSelected:YES];
}
or if you can just replace background image in - (void)buttonClicked:(id)sender

Setting an UIImage for UIControlStateHighlighted

board[i] is an array of UIButtons that I have created programmmatically, and I can't change their image for UIControlStateHighlighted:
[board[i] setImage:[UIImage imageNamed:#"block"] forState:UIControlStateNormal];
[board[i] setImage:[UIImage imageNamed:#"blockPressed"] forState:UIControlStateHighlighted];
When I press the button with the mouse in the simulator the image doesn't change. I think this is a very noob question, but I don't what the code doesn't work.
when adding button programatically do this:
add target of each same.
provide tag all button from 0 to count.
set UserInteraction to true
setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted if u want button to be highlited
Now button is pressed same method is called for all button: For example
-(void)ButtonTouched:(id)sender
{
UIButton *btntouched = sender;
NSLog(#"%#", btntouched);
[btntouched setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted];// it can be forState:UIControlStateNormal also
}
I don't think you are triggering the highlighted state. This could be because they are not set to have interaction enabled. Or there is something else missing from the way you set up your buttons.
The other thing you can try is to add a selector to each of the buttons for when they are touched, and then change the image by referencing sender for the selector function.
Assuming you've made sure your image doesn't return nil, this code should work:
[myUIButton setImage:[UIImage imageNamed:#"myHighlightedButtonImage.png"] forState:UIControlStateHighlighted];
It should work find if you call that line in your viewDidLoad().

Resources