I'm to trying change the button image after tapping on it but its not working.
- (IBAction)click:(id)sender {
UIButton *btn = (UIButton*)sender;
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateHighlighted];
}
I have seen the many solutions which given here its not working for me please tell me is there any other way to make it done.
Try UIControlStateNormal
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
Try this
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
If you are after something like "toggle button" you can use the following code
- (IBAction)click:(id)sender {
UIButton *btn = (UIButton*)sender;
btn.selected = !btn.selected;
}
previously setting up the image for the selected state in nib/storyboard or programmatically:
- (void)viewDidLoad {
[super viewDidLoad];
[self.btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateSelected];
}
This way if you press the button again it will get back to the original image.
If you want to change image to "vv.png" for good then just use:
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
use
(IBAction)click:(UIButton *)sender
instead of
(IBAction)click:(id)sender
Try this
- (IBAction)click:(id)sender {
UIButton *btn = (UIButton*)sender;
[btn setImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}
Related
I'm trying to create a uibutton to work as a checkbox. Everything is working fine but the view: I'm setting to change the view when clicked. When it is supposed to show no checkmark it's fine, but when I need to show the checkmark, it's not working. This is what happens:
And this is the code:
- (void)viewDidLoad {
[super viewDidLoad];
[self.myButton setTitle:#"\u2610" forState:UIControlStateNormal];
[self.myButton setTitle:#"\u2610" forState:UIControlStateHighlighted];
[self.myButton setTitle:#"\u2610" forState:UIControlStateDisabled];
[self.myButton setTitle:#"\u2610" forState:UIControlStateNormal];
[self.myButton setTitle:#"\u2611" forState:UIControlStateSelected];
}
//action sent to the button when touched up inside
- (IBAction)moneyButtonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
}
What is wrong?
Regards!
I just create a new project and add the following code. It works like the gif shows. You can do a compare.
- (void)viewDidLoad {
[super viewDidLoad];
self.checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkButton addTarget:self action:#selector(moneyButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.checkButton.frame = CGRectMake(50, 50, 50, 50);
self.checkButton.titleLabel.textColor = [UIColor redColor];
self.checkButton.backgroundColor = [UIColor yellowColor];
[self.checkButton setTitle:#"\u2610" forState:UIControlStateNormal];
[self.checkButton setTitle:#"\u2611" forState:UIControlStateSelected];
[self.view addSubview:self.checkButton];
}
- (IBAction)moneyButtonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
}
I think you forgot to add action event from your storyboard or by code
- (void)viewDidLoad {
[super viewDidLoad];
[self addMenuButton];
[self.myButton setTitle:#"\u2610" forState:UIControlStateNormal];
[self.myButton setTitle:#"\u2611" forState:UIControlStateSelected];
[self.myButton addTarget:self action:#selector(moneyButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)moneyButtonClicked:(UIButton *)sender {
NSLog(#"%d",sender.selected);
sender.selected = !sender.selected;
}
Button type must be Custom else you will found the tint color of button with the selection.
Outputs :
Deselected :
Selected :
Answers from ooops and Ashish Kakkad, but the problem with my code was: in the interface builder, my button was set as a System button and not as a Custom.
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.
I have added a button in tableview cell. My problem is this when I change its title on calling method pressHonkBtn:(id)sender it's not changing its title.
How to solve this.
Use your code as this :
Objective-C:
-(IBAction)pressHonkBtn:(id)sender
{
UIButton *tempBtn = (UIButton *)sender;
[tempBtn setTitle:#"YOUR_TITLE" forState:UIControlStateNormal];// YOUR_TITLE is your button title
[tempBtn setTitle:#"YOUR_TITLE" forState:UIControlStateHighlighted];
}
Swift:
someUIButton.setTitle("String To Set", forState: UIControlState.Normal)
Hope it helps you.
Please try to use this one.....It will work fine.
-(IBAction)pressHonkBtn:(id)sender
{
UIButton *btn = (UIButton *)sender;
[btn setTitle:#"title" forState:UIControlStateNormal];
}
Try this:
-(IBAction)pressHonkBtn:(id)sender
{
UIButton *btn = (UIButton *)sender;
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[btn setTitle:#"title" forState:UIControlStateNormal];
[btn setTitle:#"title" forState:UIControlStateHighlighted];
}
Since your are recieving the id as argument you need to type cast it to UIButton .
-(IBAction)pressHonkBtn:(id)sender
{
UIButton *senderBtn = (UIButton *)sender;
[senderBtn setTitle:[NSString stringWithFormat:#"Title"] forState:UIControlStateNormal];
}
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];
}
}
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.