xcode - 为 UIButton 设置图像

标签 xcode uibutton uiimage

我想向我的 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/

相关文章:

ios - xCode 无法插入新 socket

ios - 我如何将 "Upload to App Store"连接到客户端的 iTunes?

objective-c - 对于 Apple 实现(Cocoa/CocoaTouch),uint8_t 是否始终与 unsigned char 类型相同?

visual-studio - Xcode 是否有 MSVS "Edit and Continue"等效项?

iphone - 通过触摸编辑 CGImage(或 UIImage)的 alpha 然后显示的最快方法?

ios - 单击 iOS/xcode 后如何使按钮淡出?

ios - 如何删除 UIButton 选定的背景颜色?

swift - 在静态 UITableViewController 之上的 UIView

iphone - 具有大图像尺寸的 UIImage - 内存问题 - 崩溃

ios - 屏幕截图中缺少 GPUImage 输出图像