Evan
Evan

Reputation: 337

How to make multiple UIButtons connect to the same IBAction?

I need several UIButtons to all connect to the same IBAction so the buttons can all do the same thing without having to copy and paste the code. Please tell me if there is a way to do this! It might be right under my nose, but I can't find it. Thanks!

Upvotes: 0

Views: 2803

Answers (4)

Aky
Aky

Reputation: 1817

I've found that I'm usually not able to hook up multiple buttons (or even a single button, for that matter) in IB to an already existing IBAction in code, by Ctrl-dragging from the button to the IBAction in the m file. Xcode tries to create a new action in the dragged-to file, but won't connect to an existing action. [This is true for Xcode 4.6.1 and several previous (maybe all) versions.]

The approach in the accepted answer in the following link works if the IBAction is in the view controller which is the "File Owner" for the view containing the button:

Connect object in .xib to existing IBAction

However if you want your IBAction to be in the view's own class, rather than the view controller's class, then the following approach works:

  • Drag only one button onto the canvas in IB first. Control-drag from the button to the view's .m file.

  • Drop the dragged line in any vacant space in the implementation section of the code. Define a new IBAction in the little popup dialogue box. So now you have one button hooked up to an IBAction.

  • Now instead of dragging more buttons from the object library, hold down the Option key and drag out a new button from the original one. This new button will come hooked up to the same IBActions as the original one. (If you need to create lots and lots of buttons and this seems tedious, you can select a bunch of already created ones simultaneously and Option-drag to create as many more in a single go.)

Upvotes: 0

jamil
jamil

Reputation: 2437

For this You need to use set IBOutlet for Each Button or Set tag for each button if you are using Outlet then used this code .h

@interface RootViewController_Phone : UIViewController 
{
IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;
}
- (IBAction)buttonPressed:(id)sender;
-(void)CallButtonsMethod;
@end

Now in .m file

- (IBAction)buttonPressed:(id)sender{
if([sender isEqual:btn1])
{
    [self CallButtonsMethod];
}
if([sender isEqual:btn2])
{
   [self CallButtonsMethod];
}
}

-(void)CallButtonsMethod
{
 //your Code
}

Upvotes: 1

Shmidt
Shmidt

Reputation: 16664

Use IBOutletCollections.

Here is tutorial for that: http://useyourloaf.com/blog/2011/03/28/interface-builder-outlet-collections.html

Upvotes: 0

user529758
user529758

Reputation:

Simply add the same target and selector to each button:

[button1 addTarget:self
            action:@selector(buttonClicked:)
  forControlEvents:UIControlEventTouchUpInside];

[button2 addTarget:self
            action:@selector(buttonClicked:)
  forControlEvents:UIControlEventTouchUpInside];

 // etc.

Upvotes: 1

Related Questions