views:

25

answers:

1

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];

Thanks

+1  A: 

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