Question

In my xib I have taken 4 UIbuttons named button 1, 2, 3 and 4. These four buttons are connected two four different IBAction methods which perform different functions.

Now I have one more button called "Save" This has also a different IBAction method.

- (IBAction)Save:(id)sender
{

}

Now here I want to check which of the above 4 UIButtons have been clicked.

For this I tried checking this way

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

}

But this is not working. I am doing something wrong.Please help me out

Regards Ranjit.

Was it helpful?

Solution

You can set the tag values for each button in the interface builder and set actions of all buttons to this method

//set global variable flag.

   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;
}
}

for save button

- (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;
}
}

OTHER TIPS

Define a class level ivar as

UIButton *selectedBtn;

Then in you IBActions

- (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
   }
}

Can you try this:

- (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
   }

}

In your save method, check the Selected property of the other 4 buttons. If you do not want to keep the buttons in a selected state, but just want to see if they were clicked at some point, then define a property (e.g. an array) to track which buttons were clicked during the session, and check this property in your save method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top