K Hsueh
K Hsueh

Reputation: 620

set image for UIButton

I want to add an image button to my xcode project. I added a Round Rect Button using the interface builder.

In my .h file:

@property (nonatomic, retain) IBOutlet UIButton *infobutton;

In my .m file:

@synthesize infobutton;

-viewDidLoad{
infobutton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
UIImage* infobuttonImg =[UIImage imageNamed:@"info.png"];    
[infobutton setImage:infobuttonImg forState:UIControlStateNormal];
[infobuttonImg release];
}

Finally, I add a link in my interface builder linking the Round Rect Button to infobutton. However, when I run the simulator, I still got the plain Round Rect Button on my interface rather than having the button displayed as the image that I have set. I wonder if there is anything that I have missed in my code.

Upvotes: 2

Views: 9447

Answers (1)

Aaron Wojnowski
Aaron Wojnowski

Reputation: 6480

infoButton should already be populated with a button in viewDidLoad. In your code, you're overriding it.

Consider removing this line:

infobutton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

Also, infobuttonImg is autoreleased which means that this line is not required:

[infobuttonImg release];

Upvotes: 2

Related Questions