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];
}
Related
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
Hi I'm trying to change the button image by clicking the another button for example i have two buttons like button1 and button2 already this two button has images but now I'm trying to change the second button image by clicking the first button in second button the new image should replace the old image please tell me how to do it.
- (IBAction)bt1:(id)sender {
UIButton *btn = (UIButton*)sender;
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}
- (IBAction)bt2:(id)sender {
}
**[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"]** forState:UIControlStateNormal];
This above code is used to change the button image but i want know how to change the second button image by clicking image.
Thanks
You might have the IBAction property of a second button, which you can access in your implementation (.m) file with self (e.g. self.secondButtonPropertyName), now you can write the code like below:
-(IBAction)bt1:(id)sender
{
[self.btn2 setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}
first you set your both button tag..like btn1 tag =1 and btn2 tag =2
- (IBAction)bt1:(id)sender {
UIButton *btn = (UIButton*)[self.view viewWithTag:2];
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}
- (IBAction)bt2:(id)sender {
}
suppose you have two buttons btn1 and btn2
Now after them allocatng memory,
you can use below Button selector,
btn1 selector,
-(IBAction)btn1selector:(id)sender
{
[btn2 setbackgroundimage: [uiimage imagenamed:#"your image"]];
}
you can do same for the other button selector.
I am trying to update the title from u -> d on a button when it is selected. The setting to 'u' works fine but afer I click the button, it doens't update. I am putting it into the action but not sure if this is correct place.
in viewDidLoad
[self.expandButton setTitle:#"u" forState: UIControlStateNormal];
where do I put this? - not working correctly in the UIButton's action:
[self.expandButton setTitle:#"d" forState: UIControlStateSelected];
thx for any help
edit 1
Is set as a property:
#property (weak, nonatomic) IBOutlet UIButton *expandButton;
...
here's the action and how I am trying to set the selected state:
- (IBAction)expand:(id)sender {
[self.expandButton setTitle:#"d" forState: UIControlStateSelected];
You could create a function to set button title and call it on touch down:
[button addTarget:self action:#selector(buttonTouchDown:)forControlEvents:UIControlEventTouchDown];
...
EDITED
what I got is, when the button tapped, the state change to selected, and when tapped again state back to normal. you can use these trick :
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[button setTitle:#"unselected" forState:UIControlStateNormal];
[button setTitle:#"selected" forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonTap:(UIButton *)sender {
sender.selected = !sender.selected;
}
You could make a "blank" button and use a UILabel on top of it. I'm not sure of why you aren't getting updates but in my experience I usually deal with this situation in such a way.
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!
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().