iworld
iworld

Reputation: 345

one Button With Multiple Actions Using StoryBoard in ios

I have three buttons with different actions.Now I don't want to create three IBAction to my buttons.In single IBAction Method can i write the actions for those three buttons.

I am new to Xcode,Can anyone help me to do this...

Thanks in Advance....

Upvotes: 0

Views: 3794

Answers (4)

Sport
Sport

Reputation: 8975

try like this

in . h file

@property (strong, nonatomic) IBOutlet UIButton *yourbutton;

in .m

@synthesize yourbutton;

 - (IBAction)yourClicked:(id)sender {

             UIButton *resultebutton= (UIButton*)sender;
              NSString *buttontitle=resultButton.currentTitle;

                    if ([buttontitle isEqual:@"firstBtitle"]) {
                        // perform your 1st button action 
                          //call your method

                    }
                    else if ([buttontitle isEqual:@"secondBtitle"]) {
                        // perform your 2nd button action 


                    }
                    else if ([buttontitle isEqual:@"thirdBtitle"]) {
                        // perform your 3rd button action

                    }
}

Upvotes: 1

Yuvrajsinh
Yuvrajsinh

Reputation: 4584

Step 1:

Assign your all three buttons different tag in storyboard/XIB, For ex. firstButton with tag=1, secondButton with tag=2 and thirdButton with tag=3

Step 2: Define your method like this and bind all your buttons with this method

- (IBAction)buttonAction:(UIButton *)sender
{
    if (sender.tag==1) {
        NSLog(@"First Button");
    } else if (sender.tag==2) {
        NSLog(@"Second Button");
    } else if (sender.tag==3) {
        NSLog(@"Third Button");
    }
}

And your work is done.

Upvotes: 0

Protothomas
Protothomas

Reputation: 79

Please correct me, if I get you wrong:

You have three buttons and you want them to trigger the same IBAction. The IBAction itself decides what to do based on which button calls it.

This sounds to me like a perfect example for the "sender" parameter.

Create something like this:

- (IBAction)doSomeAction:(id)sender
{
    if ([sender isEqual:self.buttonOne]) {
        NSLog(@"ButtonOne");
    } else if ([sender isEqual:self.buttonTwo]) {
        NSLog(@"ButtonTwo");
    } else if ([sender isEqual:self.buttonThree]) {
        NSLog(@"ButtonThree");
    }
}

With the sender you can identify the button, which calls this method. This way, you can avoid handle tags which can be very annoying to use.

Make sure you connect all three buttons to this action - take a look at the connections inspector. This is very important and a common source for errors. If you remove any connection to an outlet or an IBAction, also check, if this connection ist remove in the Storyboard-object. If everything is in place just compare the sender with the outlets of the buttons.

Upvotes: 0

johny kumar
johny kumar

Reputation: 1270

Assign tag for buttons, and in IBAction method, check Button tag and do action, according to tag of button.

Upvotes: 1

Related Questions