Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 检查从另一个函数单击的UIButton_Ios_Ios5_Ios6_Uibutton_Sender - Fatal编程技术网

Ios 检查从另一个函数单击的UIButton

Ios 检查从另一个函数单击的UIButton,ios,ios5,ios6,uibutton,sender,Ios,Ios5,Ios6,Uibutton,Sender,在我的xib中,我使用了4个UIButton,分别命名为button 1、button 2、button 3和button 4。这四个按钮与执行不同功能的四种不同iAction方法相连 现在我又有了一个名为“保存”的按钮,它还有一个不同的iAction方法 - (IBAction)Save:(id)sender { } 现在我想检查一下上面4个UIButton中的哪一个被点击了 为此,我试着这样检查 - (IBAction)Save:(id)sender { if(sender ==

在我的xib中,我使用了4个UIButton,分别命名为button 1、button 2、button 3和button 4。这四个按钮与执行不同功能的四种不同iAction方法相连

现在我又有了一个名为“保存”的按钮,它还有一个不同的iAction方法

- (IBAction)Save:(id)sender
{

}
现在我想检查一下上面4个UIButton中的哪一个被点击了

为此,我试着这样检查

- (IBAction)Save:(id)sender
{
   if(sender == button1)
   {
      //Do this
   }
   else if (sender == button2)
   {
       //Do this
   }

}
但这是行不通的。我做错了什么,请帮帮我

问候 Ranjit.

你能试试这个吗:

- (IBAction)Save:(id)sender
{
    UIButton *pressedButton = (UIButton*)sender;

    //Check output of below statement to ensure you're getting a sender
    NSLog(@"Sender: %@", sender);

   if([pressedButton isEqual:button1])
   {
      NSLog(@"Button 1 pressed");
      //Do this
   }
   else if ([pressedButton isEqual:button2])
   {
      NSLog(@"Button 2 pressed");
       //Do this
   }

}

可以在interface builder中为每个按钮设置标记值,并将所有按钮的操作设置为此方法

//设置全局变量标志

   int flag;

- (IBAction)buttonClicked:(id)sender
{

switch ([sender tag])
{
    case 0:
         {
              flag =0;
             // implement action for first button

         }
        break;
    case 1:
        {
              flag =1;
            // implement action for second button

        }
        break;
    case 2:
        {
              flag =2;
            // implement action for third button

        }
        break;
        //so on
    default:
        break;
}
}
保存按钮

- (IBAction)save:(id)sender
{

switch (flag)
{
    case 0:
         {

             //  first button clicked

         }
        break;
    case 1:
        { 
            //  second button clicked

        }
        break;
    case 2:
        {
            //  third button clicked

        }
        break;
        //so on
    default:
        break;
}
}

在保存方法中,检查其他4个按钮的Selected属性。如果不想将按钮保持在选定状态,而只想查看它们是否在某个点被单击,则定义一个属性(例如数组)来跟踪会话期间单击的按钮,并在保存方法中检查此属性。

将类级ivar定义为

UIButton *selectedBtn;
然后在你身上

- (IBAction)button1:(id)sender {
    selectedBtn = sender // or button1
}

- (IBAction)button2:(id)sender {
    selectedBtn = sender // or button2
}

- (IBAction)button3:(id)sender {
    selectedBtn = sender // or button3
}

- (IBAction)button4:(id)sender {
    selectedBtn = sender // or button4
}

- (IBAction)Save:(id)sender
{
    //Check output of below statement to ensure you're getting a sender
    NSLog(@"Sender: %@", sender);

   if(selectedBtn == button1)
   {
      NSLog(@"Button 1 pressed");
      //Do this
   }
   else if (selectedBtn == button2)
   {
      NSLog(@"Button 2 pressed");
       //Do this
   }
    else if (selectedBtn == button3)
   {
      NSLog(@"Button 3 pressed");
       //Do this
   }
    else if (selectedBtn == button4)
   {
      NSLog(@"Button 4 pressed");
       //Do this
   }
}

这应该是可行的,虽然您最好使用
isEqual:
而不是指针比较,但我真的不认为这有什么不可行的原因。您好@H2CO3,谢谢您的回复,我通过断点检查了它,它不起作用。“保存iAction”方法仅连接到“保存”按钮。@H2CO3这将不起作用,因为“发件人”始终是他的“保存”按钮。@Ranjit您必须设置被按下的按钮的属性,然后在“保存”中检查该属性method@Hemang如果你读了他的问题,他已将其保存按钮连接到
save:
方法,该方法将导致
sender
成为
save按钮
您尚未理解我的问题。我只想检查在我的保存按钮中单击了四个按钮中的哪一个。可以单击多个按钮?发件人将始终是保存按钮,而不是“按钮1”,“button2”等,因为此方法仅是保存按钮的目标操作。