4

目前我正在使用 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];

}
4

3 回答 3

6

试试这个:-

[btn2 setImage:btn1.currentImage forState:UIControlStateNormal];

And currentBackgroundImageis anUIImage并且您将其视为 aNSString这是错误的。

于 2012-09-22T05:57:41.840 回答
1

用这个

        UIImage *img = btn1.currentImage;
        [btn2 setImage:img forState:UIControlStateNormal];
于 2012-09-22T05:59:06.443 回答
0

这是因为您正在将图像设置为按钮btn1 setImage:并访问背景图像btn1.currentBackgroundImage

所以改变你的代码测试

-(void)Button2Method
{
    [btn2 setImage:btn1.imageView.image forState:UIControlStateNormal];
}
于 2012-09-22T06:07:52.403 回答