Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 806
a1phanumeric Avatar asked Jun 23 '11 22:06

a1phanumeric


3 Answers

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.

like image 131
anoop4real Avatar answered Oct 04 '22 04:10

anoop4real


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:).

like image 31
PengOne Avatar answered Oct 04 '22 04:10

PengOne


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.

like image 20
RyanR Avatar answered Oct 04 '22 04:10

RyanR