can multiple buttons be attached to a single IBAct

2019-06-13 21:53发布

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?

5条回答
等我变得足够好
2楼-- · 2019-06-13 22:21

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

查看更多
迷人小祖宗
3楼-- · 2019-06-13 22:25

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.

查看更多
戒情不戒烟
4楼-- · 2019-06-13 22:29

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.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-06-13 22:32

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

查看更多
聊天终结者
6楼-- · 2019-06-13 22:37

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.

查看更多
登录 后发表回答