以下代码有什么问题:
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];
如果这是错误的,哪里需要纠正?为什么?
答案 0 :(得分:6)
我看到了几个问题:
UIImage *image = [UIImage imageNamed:@"icon.png"];
并删除UIImage *image = [[[UIImage alloc]init]autorelease];
和[image release];
声明。 答案 1 :(得分:4)
你可以试试这个
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(180, 10, 33, 33);;
button.tag = 1001;
UIImage *image = [UIImage imageNamed:@"icon.png"];
if (image == nil) {
NSLog(@"can't find icon.png");
} else {
[button setBackgroundImage:image forState:UIControlStateNormal];
}
//add button to the parent's view
答案 2 :(得分:0)
您确定image
不是nil
吗?