can multiple buttons be attached to a single IBAct

2019-06-13 22:21发布

问题:

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?

回答1:

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.



回答2:

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



回答3:

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:

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



回答4:

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.



回答5:

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.