xcode 为 UIButton 设置图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14616286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set image for UIButton
提问by K Hsueh
I want to add an image button to my xcode project. I added a Round Rect Button using the interface builder.
我想在我的 xcode 项目中添加一个图像按钮。我使用界面构建器添加了一个圆形矩形按钮。
In my .h file:
在我的 .h 文件中:
@property (nonatomic, retain) IBOutlet UIButton *infobutton;
In my .m file:
在我的 .m 文件中:
@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.
最后,我在我的界面构建器中添加了一个链接,将圆形矩形按钮链接到信息按钮。但是,当我运行模拟器时,我的界面上仍然是普通的 Round Rect Button,而不是将按钮显示为我设置的图像。我想知道我的代码中是否遗漏了什么。
采纳答案by Aaron Wojnowski
infoButton should already be populated with a button in viewDidLoad. In your code, you're overriding it.
infoButton 应该已经在 viewDidLoad 中填充了一个按钮。在您的代码中,您正在覆盖它。
Consider removing this line:
考虑删除这一行:
infobutton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
Also, infobuttonImg is autoreleased which means that this line is not required:
此外, infobuttonImg 是自动发布的,这意味着不需要此行:
[infobuttonImg release];