我想向我的 xcode 项目添加一个图像按钮。我使用界面生成器添加了一个圆形矩形按钮。
在我的 .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];
}
最后,我在界面生成器中添加一个链接,将圆角矩形按钮链接到信息按钮。但是,当我运行模拟器时,我的界面上仍然显示普通的圆形矩形按钮,而不是将按钮显示为我设置的图像。我想知道我的代码中是否遗漏了任何内容。
最佳答案
infoButton 应该已经用 viewDidLoad 中的按钮填充。在您的代码中,您将覆盖它。
考虑删除此行:
infobutton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
此外,infobuttonImg 是自动释放的,这意味着不需要此行:
[infobuttonImg release];
关于xcode - 为 UIButton 设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14616286/