Michelle
Michelle

Reputation: 365

SetTitle Doesn't Change the Text of a UIButton

Pretty straight forward; just trying to change the display name of my button from "Button" to "a." But the setTitle method doesn't seem to do anything. I played around with the UIState, and that didn't seem to change much.

Do I maybe need to synthesize anything?

[Button1 setTitle:@"a" forState:UIControlStateNormal];

EDIT: I did an NSLog, and the button is showing as NULL, which I've been told means it isn't connected properly. But I double-checked my Connections Inspector, looked at the H file, and everything seems to link up fine. (http://imgur.com/EFsp51U) What am I missing?

H file and Connections Inspector

Upvotes: 1

Views: 4689

Answers (8)

Sunny Shah
Sunny Shah

Reputation: 13020

Do the following:

  1. Do

    NSLog(@"%@", Button1);
    

    and check that it's not nil.

  2. Check that

    NSLog(@"%d", btn.state);
    

    should return 0.

Upvotes: 0

Michelle
Michelle

Reputation: 365

I went digging through my code, and in my viewDidLoad method, I had the following:

Button1 = NO;

I think I was trying to type out "selected" but got distracted and didn't come back to it, and so was inadvertently turning my own button off. The correct code is:

self.Button1.selected=NO;

[self.Button1 setTitle:@"a" forState:UIControlStateNormal];

Upvotes: 2

Evan
Evan

Reputation: 750

If you haven't explicitly synthesized it then you should be calling either

[_Button1 setTitle:@"a" forState:UIControlStateNormal];

or

[self.Button1 setTitle:@"a" forState:UIControlStateNormal];

Upvotes: 0

Nic Robertson
Nic Robertson

Reputation: 1208

Make sure you have synthesized your Button.

//this assumes you have a button "myButton" declared in your interface file
@implementation
@synthesize myButton;

-(void) myMethod{
    [myButton setTitle:@"The new Title" forState:UIControlStateNormal];
}
@end

You should have your button connected through IB outlets to your .h file or create the button entirely through code.

Also it is good practice to not have variable names beginning with a capital letter.

Upvotes: 0

Rukshan
Rukshan

Reputation: 8066

Your code is perfectly valid. But just to make sure everything is ok, add following extra line,

Button1.selected = NO;
[Button1 setTitle:@"a" forState:UIControlStateNormal];

Upvotes: 0

iBug
iBug

Reputation: 2352

If you have created your button in Interface Builder, then go there and change its type to custom.

enter image description here

And also check if you have connected your IBOutlet correctly.

Upvotes: 6

Envil
Envil

Reputation: 2727

I'm not sure that the problem is about thread but you may want to try to set the title on main thread:

dispatch_async(dispatch_get_main_queue(), ^{
    [Button1 setTitle:@"a" forState:UIControlStateNormal];
});

Upvotes: 0

akin
akin

Reputation: 183

  • Make sure Button1 is not nil
  • Make sure Button1.hidden = NO;

Upvotes: 0

Related Questions