按下时更改按钮背景的操作,然后从一个UIButton再次按下时返回

时间:2013-09-12 14:00:45

标签: objective-c ios6 uibutton ibaction

我正在为IOS编写类似游戏的“yahtzee”。我需要能够按下模具(按钮),如果我希望保持模具,它将变为替代图像。这很好。如果我错误地选择了它,我还需要能够取消选择该模具。我无法为我的生活做到这一点。以下是我尝试的众多选项中的两个:

选项1:

 - (IBAction)die1Button:(UIButton *)sender
   {
        if (sender)
        {
              if (self.die1 >= 1)
              {
                   [self.die1ButtonOutlet setBackgroundImage:([self.dieImage            
                                      dieSelectedImage:self.die1]) forState:
                                                    (UIControlStateNormal)];
                   self.die1Selected = YES;
              }
              else
              {
                   self.die1Selected = NO;
              }
        }
   }

 - (IBAction)die1ButtonSelected:(UIButton *)sender
   {
        if(sender)
        {
             if (self.die1Selected == YES)
             {
                  [self.die1ButtonOutlet setBackgroundImage:([self.dieImage 
                                             dieImage:self.die1]) forState:
                                                 (UIControlStateSelected)];
                  self.die1Selected = NO;
             }
        }

   }

选项2:

 - (IBAction)die1Button:(UIButton *)sender
   {
        if (sender)
        {
              if (self.die1 >= 1)
              {
                   [self.die1ButtonOutlet setBackgroundImage:([self.dieImage            
                                      dieSelectedImage:self.die1]) forState:
                                                    (UIControlStateNormal)];
                   self.die1Selected = YES;
              }
              else
              {
                   self.die1Selected = NO;
              }

              if (self.die1Selected == YES)
             {
                  [self.die1ButtonOutlet setBackgroundImage:([self.dieImage 
                                             dieImage:self.die1]) forState:
                                                 (UIControlStateSelected)];
                  self.die1Selected = NO;
             }
        }
   }

第一个选项显然不起作用,也没有。第二种选择也不会让我选择任何东西也是出于显而易见的原因。

我在这里和其他地方已经阅读了大约30篇关于UIButtons和他们的IBActions的帖子,但是无法得到这个,希望有人可以提供帮助。这是课程作业,但我们鼓励我们寻求任何来源的帮助,只要我们没有人为我们编写整个应用程序。

2 个答案:

答案 0 :(得分:0)

我想通了,最后,我是愚蠢的。以下是代码:

 - (IBAction)die1Button:(UIButton *)sender
 {
     if (sender)
     {
         if (self.die1Selected == NO)
         {
             self.die1Selected = YES;
             [self.die1ButtonOutlet setBackgroundImage:([self.dieImage
                    dieSelectedImage:self.die1]) forState:(UIControlStateNormal)];
         }else
         {

             [self.die1ButtonOutlet setBackgroundImage:([self.dieImage  
                    dieImage:self.die1]) forState:(UIControlStateNormal)];
             self.die1Selected = NO;
         }
     } 

 }

答案 1 :(得分:0)

 - (IBAction)die1Button:(UIButton *)sender{
     if(self.die1Selected)
     {             
         [self.die1ButtonOutlet setBackgroundImage:([self.dieImage
                dieSelectedImage:self.die1]) forState:(UIControlStateNormal)];}
        self.die1Selected = NO;

     }else
     {

         [self.die1ButtonOutlet setBackgroundImage:([self.dieImage  
                dieImage:self.die1]) forState:(UIControlStateNormal)];
         self.die1Selected = YES;
     }
 } 

}