Take the 2-minute tour ×

I'm probably missing the basics here... Is there a way to link more than one object to a method at a time using Interface Builder in Xcode 4.2?

I set tons of UIButtons in my UIView. All of them call just one method (let's say - (IBAction)buttonPushed:(UIButton *)aButton) that should do something different depending on the sender. I can't figure out a way to link them all with my method at a time. Any advice will be very appreciated...

N.B. I'm using Xcode 4.2 on Snow Leopard, without storyboard.

share|improve this question
I can't think of a way in IB...I don't suppose "tons of UIButtons" were considered much. :) I imagine you could do it in code, using viewDidLoad: to examine subviews, look for buttons, and set their target/action behavior. –  Phillip Mills Apr 6 '12 at 22:55
Uh!!! Do you mean using addTarget:action:forControlEvents: in a for loop? Good idea!!! –  Beppe Apr 6 '12 at 22:59
IBActions can be assigned to many buttons by simply dragging the blue line to each button. –  CodaFi Apr 6 '12 at 23:14
@CodaFi Yes, but is there a way to drag the line JUST ONE TIME and get all the buttons? I drag the line to a button of mine, release it and assign UIControlEventTouchUpInside for each button. If I try to get more than one button at a time my action is not assigned. –  Beppe Apr 6 '12 at 23:24
In my point of view,.XIB files will create xml file to produce an output exactly like what you want. If you try to select all buttons and drag it to a single method,how it can create a xml file as you need.Its not possible.If you connect separately, you can achieve that.Otherwise use for loop in code as @codaFi said. –  Dinesh Raja Apr 7 '12 at 13:57
add comment

3 Answers

This should just work. Namely, you

  1. Have your IBAction defined
  2. You attach the "touch up inside" (or other desired action) to the method for each button
  3. You set the tag value on each button
  4. In your IBAction you get the tag from sender and act accordingly

    - (IBAction)buttonPressed:(id)sender;
        NSInteger tag = [sender tag];
        // switch statement or some other check against tag value
share|improve this answer
This is actually what I'm doing, but my goal is to speed up point 2 and to avoid linking for each button... (Anyway, I appreciate your time and effort) –  Beppe Apr 6 '12 at 23:19
Ah, misunderstood your question. I don't believe IB allows for multi-select in this way. You could accomplish this via code of course, but that too is not your question. Goodluck –  Ryan Crews Apr 6 '12 at 23:36
@RyanCrews didn't mention anything wrong.I don't know who downvoted him.But i will give upvote to him. Beppe didn't mention he used tag already.So this answer is also correct only. –  Dinesh Raja Apr 7 '12 at 13:58
add comment

A for-loop might be a good idea. Keep an NSArray of your buttons and do something like this:

for (int i = 0; i < [myArray count];  i++) {
    [(UIButton*)[myArray objectAtIndex:i] addTarget:self action:@selector(buttonPushed:) forControlEvents:UIControlEventTouchUpInside];
    [(UIButton*)[myArray objectAtIndex:i]setTag:i];

(coming from my iPhone, this is the best I could do. It's a little rough and might not compile, but you get the gist).

share|improve this answer
add comment
up vote 0 down vote accepted

It seems it is not possible to link more than one object at a time in Xcode 4.2 using IB.

I ended up with a for loop in my code, as first suggested by Phillip Mills (+1 to his comment).

Here my code:

for (UIButton *aButton in [self.view subviews]) {
    [aButton addTarget:self

Thanks everybody for your help!!!

share|improve this answer
add comment

Your Answer


