我在iphone应用里添加了两个按钮,每个按钮都有背景图片。我想让用户点击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];
}
我解决了。
[btn2 setImage:btn1.currentImage forState:UIControlStateNormal];
代码中currentBackgroundImage
属于UIImage
,不能把它当成NSString
。
也可以试试
UIImage *img= btn1.currentImage;
[btn2 setImage:img forState:UIControlStateNormal];