1

Super simple question but I cannot find anywhere than answers this. Whenever I google it I just get answers related to whether it is possible to connect multiple buttons to the same method.

I have a UI with 60 or so buttons on it that all need to connect to the same method and I know that I can ctrl+drag to link them to an method. What I was wondering is whether I can select all 60 of them and drag them at the same time to link them to a method.

Sam Parmenter
  • 1,831
  • 2
  • 11
  • 17
  • 3
    I can think of a turnaround solution for you. Since there is no short cut for creating 60 buttons on Story board as well, why don't you create your first button, give it connection to the method in normal way, after that, do the remaining buttons copy paste of the first button. You will not have to set action of the remaining buttons in that way. – Prasad Sep 03 '14 at 18:47
  • You could edit the XML xib/nib/storyboard directly. Cut and paste. – i_am_jorf Sep 03 '14 at 18:48

2 Answers2

1

The XML for the button connection looks something like this, if you open up the nib (or xib or storyboard) in a text editor:

<connections>
    <action selector="yourSelector:" destination="9AB-mn-cd1" eventType="touchUpInside" id="q9A-8X-6bF"/>
</connections>

The selector is the selector you want to call, destination is the target object's id, eventType is the event type (obviously), and id is the id of the button. You can cut and paste that into each button element and then just update the id field.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • I think I understand what you are saying but is this likely to be as fast as ctrl+dragging from the storyboard to the editor. Having to find each buttons id and manually add it would be rather slow no? – Sam Parmenter Sep 03 '14 at 19:08
  • Could be. I'd prefer to use the keyboard for repetitive tasks, and if you're good with regex SublimeText can probably do this quickly. Again, learning the skill may take longer, but save you time in the long run. I'm sure vi and emacs have fancy ways to do this too. – i_am_jorf Sep 04 '14 at 00:11
  • Just pawn it off on the interns. :) – i_am_jorf Sep 04 '14 at 00:11
  • Haha, finding interns is going to slow down the process even more! – Sam Parmenter Sep 04 '14 at 09:57
1

There is a way to have 60 buttons all connected to the same IBAction very quickly, though this is only quick if every button is mean to look the same.

If you add a UIButton to your storyboard or Xib then CTRL-Drag to the header file and connect as an action. Now if you copy this UIButton and paste, the copied buttons will have also copied the IBAction connection.

On IB, select the UIButton -> press cmd + C to copy, then cmd + V to paste.

You will still have to spend time placing all of the pasted UIButton's though, but if they are meant to look the same, then at least it should be quicker than CRTL-dragging each individual button.

Hope this helps

Jim Tierney
  • 4,078
  • 3
  • 27
  • 48