将不同的UIButton抽头组合在一起

时间:2014-01-17 05:10:07

标签: ios objective-c uibutton

我正在尝试创建一个应用,其中用户4 buttons让我们说labeled 1到4.如果用户点击button-1则会发生一些事情(我知道如何做这部分)。但是,如果用户点击按钮1然后按下按钮2,我想要发生新的事情。有没有办法结合点击,如果是这样我会非常感谢帮助。

2 个答案:

答案 0 :(得分:0)

为此你可以使用[btn1 isSelected]方法即。您可以检查是否选择了btn1,然后按下btn 2,然后执行所需的操作。 您需要做的就是在点击按钮时选择并取消选择按钮,并检查其他按钮动作的状态。

  - (IBAction)btn1Action:(id)sender {
        UIButton *btn1 = (UIButton *)sender;

        if ([btn1 isSelected]) {
       [btn1 setSelected:NO];
      }
      else{
         [btn1 setSelected:NO];
         }
      }

- (IBAction)btn2Action:(id)sender {

// Button 1 outlet is myBtn1 (Suppose)
        UIButton *btn1 = (UIButton *)sender;

        if ([myBtn1 isSelected]) {
       // Pressed after button 1
      }
      else{
         // Pressed after button 2
         }
      }

答案 1 :(得分:0)

下面分享的代码片段,让您了解如何触发多按钮点击组合的事件。 在这种情况下,我只使用了一个按钮组合2.And我在事件结束后禁用所选按钮。

-(IBAction)clickButton:(UIButton *)sender
{

switch (sender.tag) {
    case 0:
    {
        if(_button2.isSelected)
        {
            //do task for combination of 2,1

            _button2.selected=NO;
            _button1.selected=NO;
        }

        if (_button3.isSelected)
        {
            //do task for combination of 3,1

            _button3.selected=NO;
            _button1.selected=NO;
        }

        if(_button4.isSelected)
        {
             //do task for combination of 4,1

            _button4.selected=NO;
            _button1.selected=NO;
        }

        else
        {
            //do task for button1 alone

            _button1.selected=NO;

        }

    }
    break;
    case 1:
    {
        if(_button1.isSelected)
        {
            //do task for combination of 1,2

            _button1.selected=NO;
            _button2.selected=NO;
        }

        if (_button3.isSelected)
        {
            //do task for combination of 3,2

            _button3.selected=NO;
            _button2.selected=NO;
        }

        if (_button4.isSelected)
        {
            //do task for combination of 4,2

            _button4.selected=NO;
            _button2.selected=NO;
        }

        else
        {
            //do task for button2 alone

            _button2.selected=NO;

        }
    }
    case 2:
    {
        if(_button1.isSelected)
        {
            //do task for combination of 1,3

            _button1.selected=NO;
            _button3.selected=NO;
        }

        if (_button2.isSelected)
        {
            //do task for combination of 2,3

            _button2.selected=NO;
            _button3.selected=NO;
        }

        if (_button4.isSelected)
        {
            //do task for combination of 4,3

            _button4.selected=NO;
            _button3.selected=NO;
        }

        else
        {
            //do task for button3 alone

            _button3.selected=NO;

        }

    }
    case 3:
    {
        if(_button1.isSelected)
        {
            //do task for combination of 1,4

            _button1.selected=NO;
            _button4.selected=NO;
        }

        if (_button2.isSelected)
        {
            //do task for combination of 2,4

            _button2.selected=NO;
            _button4.selected=NO;
        }

        if (_button3.isSelected)
        {
            //do task for combination of 3,4

            _button3.selected=NO;
            _button4.selected=NO;
        }

        else
        {
            //do task for button4 alone

            _button4.selected=NO;

        }
    }
    case 4:
    {
        _button1.selected=NO;
        _button2.selected=NO;
        _button3.selected=NO;
        _button4.selected=NO;
    }

    default:
    break;
   }

 }

希望它能帮助!!!