Issues in setting UIButton title in its click event - iphone

I'm trying to change the title of a button when the user press on it with the following code:
- (IBAction) hideKB: (UIButton *) sender {
sender.titleLabel.text = #"↓";
}
But when I click the app crashes and I can't understand why.
Removing the sender stuff, the button works with no problems.

The correct method signature for an action is
- (IBAction)action:(id)sender
Your app is crashing because your object is being sent a message it doesn't understand.
Try replacing your code with something along the lines of:
- (IBAction)hideKB:(id)sender {
UIButton *button = (UIButton *)sender;
[button setTitle:#"↓" forState:UIControlStateNormal];
}
As you may notice I've also changed the line that sets the button title. This is because you should never manipulate a UIButton's titleLabel property directly, but rather you should be using the appropriate setter method as shown above.
Edit: To clarify, most controls will allow you to use dot notation to edit the text property of their titleLabel. However, UIButton instances support different titles (as well as images and background images) depending on the state they're in.
If you're wondering why a UIButton could be in one of several different states, a good example you often see is buttons that are "greyed out". What this means is that those buttons are in the UIControlStateDisabled state. You can find a list of all the possible states for a control in the documentation.

Try using
- (void)setTitle:(NSString *)title forState:(UIControlState)state
e.g.
[sender setTitle:"↓" forState:UIControlStateNormal];

you can try with:
[(UIButton *)sender setTitle: #"↓"" forState: UIControlStateNormal];
For more information, you can refer to setTitle:forState: in UIButton Class Reference
Hope this helps.

Related

set UIButton's image in interface builder for differen state

I am facing very strange issue.
In interface builder I am trying to set images for "normal" state config and for "selected" state config. but by clicking the button my image is not getting changed.
even I write the code in viewDidLoad method.
[btnCheckBoxMale setImage:[UIImage imageNamed:#"blankcheckbox.png"] forState:UIControlStateNormal];
[btnCheckBoxMale setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateSelected];
but my images are not getting changed on cliking on it
can anyboday say what is the problem?
Set the image to different states in interface builder instead of setting it in code. Please see the screenshot for how to set it. Try this & if you are facing any problem then ask me.
It will work if you set manually in button action:
btnCheckBoxMale.selected = YES;
Because by default UIButton has only two states - Normal and Highlighted. And other states like selected and disabled you have to control manually.
just set selected when you call the method on button click event.. for example..
-(IBAction) btnCheckBoxMale_Clicked:(id)sender{
[btnCheckBoxMale setSelected:YES];
}
-(IBAction) btnCheckBoxMale_Clicked:(id)sender{
UIButton * btnCheckBoxMale = (UIButton *)sender;
[btnErase setSelected:! btnCheckBoxMale];
}

Change UIButton image when button pressed

I'm trying to change the image button just in the moment that the button is pressed. When released, i want go back to the original.
I read some posts here, but it's not working.
Is it possible do it in the GUI without programming?
I tried this:
-(IBAction) toggleUIButtonImage:(id)sender{
if ([sender isSelected]) {
[sender setImage:unselectedImage forState:UIControlStateNormal];
[sender setSelected:NO];
}else {
[sender setImage:selectedImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
}
But what kind of sender events should i associated the function toggleUIButtonImage ?
Thanks
You can do it in interface builder by giving different settings to each button state config:
Or you can also change the 'settings' of the button in code by using 'UIButton' setImage:forState method.
[myButton setImage:highlightedImage forState:UIControlStateHighlighted];
Usually, you should do it by 'configuring' the button as described above and not by changing the sub views of the button manually as a response to user interaction. The reason it doesn't work for you is the inherent hidden behaviour of UIButton. You manually make the change of a subview of the button as a response to user interaction, but your change is changed back to what was defined in the settings of your button (according to the button's state). So it seems like your code doesn't do anything at all.
in the ViewDidLoad Method
set the property of
[[button setImage:[UIImage ImageNamed:#"unselectedImage.png"] forState: UIControlStateNormal];
[[button setImage:[UIImage ImageNamed:#"selectedImage.png"] forState: UIControlStateHighlighted ];
This can be done with Interface Builder, no code necessary.
Select your button in IB, go to the right sidebar, change the Type to Custom, make sure State Config is Default, and type in the name of your "regular image" reference in the Background Image text field.
Then change State Config to Highlighted, and type in the name of your "pressed image" reference in the Background Image text field.
In IB, you can set the image for the Highlighted (not Selected) state of the button. That should do it.
Look in the Button section of the Utilities pane, and select Highlighted from the State Config drop-down.

UIButton setImage not working

I'm losing my mind over this one. I have UIButton connected as Outlet. I want to change it's image. In the viewDidLoad function, I try to set the image like so:
[button1 setImage:[UIImage imageNamed:#"house.png"] forState:UIControlStateNormal];
Nothing happens. However, if I use ImagePicker, it somehow works. In the ImagePickers function "didFinishPickingImage", I repeat the exactly same command as above for setting image and it works, the image is displayed normally.
I've been thinking that maybe the button's image is not refreshing itself, but trying to call [button1 setNeedsDisplay], or [button1.imageView setNeedsDisplay] or even [button1.imageView.image setNeedsDisplay] does nothing.
Does anyone have any idea about this? I've wasted two hours by now and I'm really getting frustrated, because I'm certain that the reason must be so stupid it's unbelivable.
Please check your button type.
If your button type is "System" you should change it to "Custom"
Two things to do here:
#1, change your code to:
[button1 setImage:[UIImage imageNamed:#"house.png"] forState:UIControlStateNormal];
and #2
make sure your UIImage returns a valid image.
In other words,
UIImage * imageToSet = [UIImage imageNamed: #"house.png"];
if(imageToSet)
{
[button1 setImage:[UIImage imageNamed:#"house.png"] forState:UIControlStateNormal];
} else {
// if you see this line in your console, then you know where to look for a problem
NSLog( #"why is my image object nil?");
}
For anyone who will find themselves in a similar situation; I was using the mentioned button inside a custom cell for table view. However, the cells aren't loaded right away (i.e. inside viewDidLoad/viewWillAppear etc.), but inside the tableView:cellForRowAtIndexPath function. All my button variables were nil at that point. My solution was to load the image after the cell has been created and it has worked straight away afterwards.
Here are a couple additional notes. UIButton setImage: forState: is the correct method:
[button1 setImage:[UIImage imageNamed:#"house.png"] forState:UIControlStateNormal];
imageNamed is a great convenience method for grabbing the image, but make sure it returns an object. Forgetting to include the image file in the current target gets me all of the time.
[UIImage imageNamed:#"house.png"]
You also don't need the extension. The SDK works this out.
[UIImage imageNamed:#"house"]
And even though UIControlState is a bit mask, you cannot OR the values together. This does not not not work and I must must must remember this because it gets me every time I use the call.
// NO NO NO
[button1 setImage:[UIImage imageNamed:#"house"] forState:UIControlStateNormal|UIControlStateSelected];
// YES
[button1 setImage:[UIImage imageNamed:#"house"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"house"] forState:UIControlStateSelected];
Was getting the same problem when UIbutton was in UITableViewCell.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"detail.png"] forState:UIControlStateNormal];
should work in UITableViewCell
Glad to know that the problem is solved but none of the answers actually addresses the problem.
Generally, if the code is correct, then the error shifts from "syntax error" to the "logical error" category. There is no definite guide to handle it but here is a small checklist that can help anyone to troubleshoot their code.
1) The IBOutlet is not connected or renamed.
2) Make sure that the UIButton type is custom.
3) The image is set (more than once), somewhere else in the code.
4) You are trying to set the Image through any sync method which is not using the main thread. (There are several threads on that alone).
5) If you are nesting the images as subviews, make sure the problem isn't with their parent view.
6) If it is appearing in Simulator but not in the device, It's a lettercase (Uppercase, lowercase difference in the code and the filename.
7) Modern simulators works with very low GPU memory on older machines. You can tweak the simlator options to make it appear. E.g (Command + Right Arrow) twice to change the simulator orientation and your changes will appear.
There are few other but common pitfalls where developers usually overlook
1) The image doesn't exist OR you are making a spelling mistake.
2) The filetype is different than mentioned. (JPG, PNG).
Last thing you'll want to check - make sure you're setting the image on the main thread! Just ran into an issue where I was setting a button image in a callback from ALAssetLibrary (fetching images from photo library). The image wouldn't update until I tapped the button. Turns out the callback was happening off main...
for those having issues with system images not appearing, just use systemName: instead of named
if let starImg = UIImage(systemName: "star.fill") {
button.setImage(starImg, for: .normal)
}
It's for ios13 and later...
For me it was to move the line:
[button1 setImage:[UIImage imageNamed:#"house.png"] forState:UIControlStateNormal];
from awakeFromNib method
to viewDidLoad method
I guess,
don't create like this
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
by this create method ,UIButton is create by use default type,so your background image is not work.
try this,
UIButton *nextButton = [[UIButton alloc] initWithFrame:CGRectMake(9, 154, 300, 48)];
or other,except buttonWithType
This is happening because the state of the Button doesn't stays selected once clicked by default.
I solved it by making an IBaction on that button click and toggling the selected state.
-(IBAction) checkButtonClicked:(id)sender{
checkButton.selected = !checkButton.selected;
}
In my case, it is drawn by custom animation code and not set by the image, so it doesn't work by setting any image, it has to change the animation code.
With images, you have to import them into the project not as references but as copies. So when you go to add files to project, choose "Create groups" instead of "Create folder references". Also make sure "Copy items if needed" is checked.
This is confusing because importing as references works when you are using images as textures (e.g. for Sprites). However, it doesn't work when you want to use images for buttons (e.g. for using image name in [UIImage imageNamed:#"house.png"]
I faced this same issue of the image not being shown on the UIButton.
I had to change this line -
myButton.setImage(<my_UIImage_object>, for: [.normal, .focused])
to this -
myButton.setImage(<my_UIImage_object>, for: .normal)

How to set UIButton title to null or an empty string

TL;DR - My question really is: How to I set a UIButton's title to an empty string, or null after it has already been assigned a previous title.
I have a UIButton that accepts a string generated based on the text from an NSString variable as it's title using the following:
NSString *MyStringVariable = #"HELLO";
...
[myButton setTitle:MyStringVariable forState:UIControlStateNormal];
For the sake of this question, let's just assume I have "HELLO" set on my NSString variable. This then makes my UIButton have the title "HELLO", obviously...
I then have another button which removes this title from the UIbutton, like so:
[myButton setTitle:#"" forState:UIControlStateNormal];
Which does hide the UIButton's title (i.e. it now just looks like an empty UIButton), but upon further inspection, the title does infact still exist. If I add the following to my UIViewController:
- (void)viewWillAppear:(BOOL)animated{
NSLog(#"Title: %#", myButton.titleLabel.text);
}
and navigate to and fro from this view, the title continues to get logged in the console, even though I've set the text to #"".
I have tried the following methods to make the title truly go away:
[myButton setTitle:#"" forState:UIControlStateNormal];
[myButton setTitle:NULL forState:UIControlStateNormal];
[myButton setTitle:nil forState:UIControlStateNormal];
myButton.titleLabel.text = #"";
The weird thing is, if I set the title to #" " (with a space):
[myButton setTitle:#" " forState:UIControlStateNormal];
the title does update correctly, and the space is then logged to the console, rather than the old title.
My question really is: How to I set a UIButton's title to an empty string, or null after it has already been assigned a previous title.
I too faced the same issue. I was setting the title of the button to #"" for all the states, and at a later point when I check the titlelabel.text it gives me the previous title which is not #"". So I printed out all the possible values of a button as suggested by PengOne and found that the properties titleForState and currentTitle give the updated/correct values.
So instead of checking the titleLabel.text, try checking the currentTitle or titleForState.
I still haven't figured out why the titleLabel is not updated properly.
I believe this has to do with how the UIButton updates its titleLabel property when setTitle:forState: is called.
For example, if you try to set the title of a button by directly accessing the titleLabel.text property, it does not work (check out the myriad questions on SO about this).
If you truly want to understand what's going on, then you should look at both the titleLabel.text and the current state of the button. One way to do this is to log titleLabel.text, currentTitle, and titleForState: in viewWillAppear, viewDidAppear, viewWillDisappear, and viewDidDisappear. The first two (titleLabel.text, currentTitle,) should coincide, and the third should be what you expect (titleForState:).
This sounds like a bug in the SDK, create a bugreport.
If you really need a button without a title, you'll probably have to create one yourself. A UIView with it's layer set to have a cornerRadius (and hence be rounded), which responds to user interaction should do it. Also, check the three20 toolkit, they may have a custom button or two in there you could use to save some time.
I have seen the same issue but in a different context. I have been using NSLocalizableString to change the text in a button.
It appears that setting the UIButton.title changes its UIButton.currentTitle but not its UIButton.titleLabel.text.
Though it may not make much of a difference for your application, the fact that titleLabel.txt and currentTitle are different turns to be more of a "be aware" than an implementation challenge.

Two Problems I'm having with UIButton and UIView

I haven't been programming on the iPhone for very long, but I'm picking it up slowly by googling problems I get. Unfortunately I haven't been able to find an answer for these.
I have started a new View-based application in Xcode 3.2.2 and immediately added the following files: myUIView.m and myUIView.h, which are subclasses of UIView. In Interface Builder, I set the subclass of the default UIView to be myUIView.
I made a button in the drawRect method.
Problem one: The title of the button only appears AFTER I click the screen, why?
Problem two: I want the button to produce the modalview - is this possible?
The code is as follow:
#import "myUIView.h"
#implementation myUIView
- (void)drawRect:(CGRect)rect {
// Drawing code
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0,0,100,100);
[button setTitle:#"butty" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
-(void)buttonPressed:(id)sender{
NSLog(#"Button pressed");
//present modal view somehow..?
}
I can't see how to post attachments, but if anyone thinks it will help I can upload the source.
Many thanks,
Andy
drawRect: is for custom drawing. You won't often need to do that. Your button won't be drawn until it gets sent drawRect:, probably in the next iteration of the run loop (ie, when you click on the view).
If you want a button on your view, either drag it there using IB, or move the code from drawRect: to viewDidLoad.
You present a modal view from a view controller by:
[self presentModalViewController:viewController animated:YES];
Do this in the action method your button sends to your view controller. This should indicate to you that you need to take a look at your application design.