Trying to update the title a UIButton in response to being selected - ios

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.

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

Displaying UIButton programmatically

I'm trying to display a button to the view programmatically with this code:
- (void)viewDidLoad {
_detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
_detailButton.frame = CGRectMake(23, 294, 72, 37);
[_detailButton setTitle:#"UIButton!" forState:UIControlStateNormal];
[_detailButton addTarget:self
action:#selector(showPop:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_detailButton];
}
I have the button declared in the .h file like this:
#property (strong, nonatomic) UIButton *detailButton;
The code does not throw any errors or warnings, but when I run there is no button displayed.
I check your code in my project its working fine i think in your case there are some other view that cover this button so check that your code has no problem at all.
Your button probably covered by another view. Just make sure its on top of other views.
In viewWillAppear add the following line of code:
[self.view bringSubviewToFront:self.detailButton];
If your button's type is of UIButtonTypeCustom, the button's default title color is white, so you can't see it if your view's background color is white.
[_detailButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

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

How to pass a variable to a method that is called when UIButton is triggered?

I have a programmatically created UIButton with a UIScrollView. When this button is pressed, it triggers an action, but besides just triggering the action, I want the button to send a (int) variable to the method as well.
Here is my button:
UIButton *btn15 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn15 setTitle:#"Cool title" forState:UIControlStateNormal];
[btn15 setFrame:CGRectMake(165, 770, 145, 145)];
[btn15 addTarget:self action:#selector(selectFav) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn15];
...So this is my 15th button within this scroll view. I want to write the method 'selectFav' to handle a button press. I would like each button to send to the method it's number, and then have the method print the number out to NSLog. Since this example is button 15, I want to have the the number 15 somehow passed to selectFav.
I'm assuming this is an easy thing to do, but can't seem to make it work. Thanks for any suggestions!
You can use UIView's tag property into the button, and then take the value out of the button once it's pressed.
something like:
- (void)yourAction:(UIButton *)sender{
NSInteger value=[sender tag];
}
This is easy to do in BlocksKit. BlocksKit allows you to attach blocks as event handlers.
UIButton *btn15= ...;
[btn15 addEventHandler:^(id sender) {
[self selectFav:15];
} forControlEvents:UIControlEventTouchUpInside];
- (void)mySelectFav:(NSUInteger)buttonId {
...
}
This is how I would do it. Set a tag property of the button and then get to the property in button action.
-(void)myMethod
{
UIButton *btn15 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn15 setTitle:#"Cool title" forState:UIControlStateNormal];
[btn15 setFrame:CGRectMake(165, 770, 145, 145)];
[btn15 addTarget:self action:#selector(selectFav:) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn15];
btn15.tag = 15;
}
-(void)selectFav:(id)sender
{
UIButton *button = (UIButton *)sender;
NSLog(#"%d", button.tag); //this will print 15. Store it in a int variable
}
You can use the tag of the UIButton being passed as mentioned by #J2theC, or you can also add a property to UIButton: Subclass UIButton to add a property
As you are passing the UIButton to the selector, you can identify which UIButton is being passed from the tag or the property.
Also do not forget to update your addTarget method implementation to include : in the selector parameter being passed:
[btn15 addTarget:self action:#selector(selectFav:) forControlEvents:UIControlEventTouchUpInside];

Resources