我在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
。