3

How does one set the image of a UIButton from within the code?

I try the following but it just leaves the button as a white square.

[A1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"m1.10001.png"]] forState:UIControlStateNormal];
Cœur
  • 37,241
  • 25
  • 195
  • 267
some_id
  • 29,466
  • 62
  • 182
  • 304

1 Answers1

4

Your image construction seems a bit overly complicated, but it should work. Don't forget to use the correct button type (custom):

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"m1.10001.png"] forState:UIControlStateNormal];

With newer SDKs you can omit the .png. Make sure that the image actually exists (you can store it in a temporary variable and check for nil, for example).

And of course make sure that the button exists (or is bound), i.e. non-nil, when setting the image.

Eiko
  • 25,601
  • 15
  • 56
  • 71