单击另一个按钮时图像更改

时间:2013-10-08 13:11:39

标签: iphone ios objective-c

我正在为拼图匹配游戏编写代码。我使用UIButton定义了UIImage,我添加了一个目标来更改UIImage的{​​{1}}。 我希望这张照片一直显示,直到我点击另一个UIButton或同一UIButton,我该怎么做?!

UIButton

UIButton

然后方法:

mybutton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mybutton1 addTarget:self 
              action:@selector(method:) 
    forControlEvents:UIControlEventTouchDown];
question = [UIImage imageNamed:@"puzzle.jpg"];
[mybutton1 setBackgroundImage:question 
                     forState:UIControlStateNormal];

1 个答案:

答案 0 :(得分:0)

试试这种方式..

Button*  mybutton1= [UIButton buttonWithType:UIButtonTypeCustom];
[mybutton1 addTarget:self 
              action:@selector(method:) 
    forControlEvents:UIControlEventTouchDown];
question = [UIImage imageNamed:@"puzzle.jpg"];
[mybutton1 setBackgroundImage:question 
                     forState:UIControlStateNormal];

-(void) method: (UIButton*) sender{
    [sender setBackgroundImage:[UIImage imageNamed: @"cheerful-cat.jpg"] 
                      forState:UIControlStateNormal];
}