为UIButton设置图像

时间:2013-01-31 00:11:40

标签: xcode uibutton uiimage

我想在我的xcode项目中添加一个图像按钮。我使用界面构建器添加了一个Round Rect Button。

在我的.h文件中:

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

在我的.m文件中:

@synthesize infobutton;

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

最后,我在界面构建器中添加一个链接,将Round Rect Button链接到infobutton。但是,当我运行模拟器时,我仍然在我的界面上得到了普通的圆形矩形按钮,而不是将按钮显示为我设置的图像。我想知道我的代码中是否有任何遗漏。

1 个答案:

答案 0 :(得分:2)

infoButton应该已经在viewDidLoad中填充了一个按钮。在您的代码中,您将覆盖它。

考虑删除此行:

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

此外,infobuttonImg是自动释放的,这意味着不需要此行:

[infobuttonImg release];