简体   繁体   中英

Attach multiple buttons to IBAction without dragging each one from the storyboard

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM