【发布时间】:2012-02-24 04:04:56
【问题描述】:
以下代码有什么问题:
UIButton *button = [[UIButton alloc]init];
CGRect frame = CGRectMake(180, 10, 33, 33);
button.frame = frame;
button.tag = 1001;
UIImage *image = [[[UIImage alloc]init]autorelease];
image = [UIImage imageNamed:@"icon.png"];
[button setBackgroundImage:image forState:UIControlStateNormal];
[image release];
[button release];
如果这是错误的,哪里需要纠正,为什么?
【问题讨论】:
-
如果有错吗?你试过了吗?你应该知道它是否有效。
-
在 IB 中这样做会容易得多。创建一个按钮,将类型设置为自定义,然后设置图像。您是否有理由需要以编程方式执行此操作?
-
它对我有用。但有人告诉我
UIImage *image = [[[UIImage alloc]init]autorelease]; image = [UIImage imageNamed:@"icon.png"];是错误的做法。 -
@Osiris 我的要求是以编程方式完成
-
@XaviValero 查看 dbrajkovic 和 Looyao 的答案。你还应该检查你的代码。
标签: iphone ios uibutton uiimage