目前我在iPhone应用程序中工作,我为两个按钮创建了两个按钮并设置了图像,当用户触摸button2然后在button2内显示button1图像时,我尝试了我的关卡,但我无法修复此问题,请任何人帮助我
先谢谢
我试过了:
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setImage:[UIImage imageNamed:@"4.png"] forState:UIControlStateNormal];
btn1.frame=CGRectMake(61, 60, 50, 50);
[btn1 addTarget:self action:@selector(Button1Method) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:@"8.png"] forState:UIControlStateNormal];
btn2.frame=CGRectMake(112, 60, 50, 50);
[btn2 addTarget:self action:@selector(Button2Method) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
-(void)Button2Method
{
[btn2 setImage:[UIImage imageNamed:btn1.currentBackgroundImage] forState:UIControlStateNormal];
}
答案 0 :(得分:6)
试试这个: -
[btn2 setImage:btn1.currentImage forState:UIControlStateNormal];
并且currentBackgroundImage
是UIImage
,您将其视为NSString
这是错误的。
答案 1 :(得分:1)
使用此
UIImage *img = btn1.currentImage;
[btn2 setImage:img forState:UIControlStateNormal];
答案 2 :(得分:0)
这是因为您要将图片设置为按钮btn1 setImage:
并访问背景图片btn1.currentBackgroundImage
所以更改您的代码经过测试
-(void)Button2Method
{
[btn2 setImage:btn1.imageView.image forState:UIControlStateNormal];
}