x4h1d x4h1d - 2 months ago 34
Swift Question

Method name gets changed in storyboard connections inspector

I created a method for a button using

Swift 2.3
in
Xcode 8
. let's say

@IBAction func testAction(sender: UIButton) {
print("\(sender.tag)");
}


but in
Connections inspector
of storyboard editor it shows as
testActionWithSender:
, which I know is not the correct method name but I have no problem with that until it crashes with



[TestProject.ViewController testActionWithSender:]: unrecognized selector sent to instance 0x7ff3f3e050e0


on button tap.


So why does the method name changes in
Connections inspector
and how do I fix it?

Answer

By Using this workaround you can add existing action and also can connect multiple buttons to a single action.

I think there is a bug in Xcode8. You can add multiple/Single button to a single action /function by changing sender to _ sender

eg :- Normal Button

   @IBAction func huu(sender: UIButton) {

   }

You can't add multiple buttons or single button to this action you need to simply change like this and then you can add multiple buttons by using drag and connect from storyboard.

 @IBAction func huu(_ sender: UIButton) {

 }

After connecting IBOutlets Xcode will show a warning like this :-

enter image description here

To remove this warning simple delete _ sign from the action/function. Make sure that to delete _ after connecting your IBOutlets

Hope that this will gonna help you! :)

Comments