Onicha21
Onicha21

Reputation: 161

can multiple buttons be attached to a single IBAction is Xcode 8?

I have tried to connect several buttons to a single IBAction in Xcode 8 however it does not work. I tried the practices outlined here Xcode8 cannot connect multiple/single uibuttons to single @IBAction in swift file still to no avail. can anyone shed some light as to why or if it is simply still just a bug?

Upvotes: 0

Views: 2802

Answers (5)

Pegasus Roe
Pegasus Roe

Reputation: 19

change your parameter from (_ sender: Any) to (_ sender: UIButton), and you're good to go.

Upvotes: 2

Onicha21
Onicha21

Reputation: 161

I was able to resolve the issue by upgrading to Xcode 8.1 and including an _ before the sender argument. I believe this is just a bug from Xcode 8 because I tried the latter before upgrading and I was still unable to connect.

Upvotes: 0

Ren
Ren

Reputation: 19

Double check if you want to add action to a real button Check if when you wrote the action, you used some customized class instead of UIButton, and the class of your button does not match.

Upvotes: 0

EricS
EricS

Reputation: 9768

Works fine for me in Xcode 8.1 on a new Swift 3 project. You have to control-drag from the button to the center side of the method to select it if it already exists rather than drag to the left side.

enter image description here

Upvotes: 4

matt
matt

Reputation: 535231

There is no reason why several buttons should not connect to a single action function in your code. I had no trouble doing this when I tried it, as you can see:

enter image description here

However, if the nib editor interface won't let you form the connection, configure the action-target of the buttons in code.

Upvotes: 0

Related Questions