Anthony D.
Anthony D.

Reputation: 193

Xcode8 cannot connect multiple/single uibuttons to single @IBAction in swift file

I am using Xcode 8 beta. I cannot drag multiple UIButtons from storyboard to connect to a single @IBAction in swift file. Bug? Thanks for your help.

Upvotes: 14

Views: 4860

Answers (10)

Suneel Gunupudi
Suneel Gunupudi

Reputation: 57

The Below IBAction will work only for one Button.

@IBAction func singleButton(sender: Any) {
   // Write your logic
}

The Below IBAction will work with multiple buttons.

@IBAction func multipleButton(sender: UIButton) {
   // To find a particular button from an array of buttons
   if sender.tag == 1{
     // Write your logic
   }else if sender.tag == 2{
    // Write your logic
   }

}

Upvotes: 1

Suneel Gunupudi
Suneel Gunupudi

Reputation: 57

I tried many times, at last, i got the result as,

Normal UIAction

@IBAction func selectBtn(_ sender: Any) {

   }

for Multiple Buttons with same UIAction method, try below method,

@IBAction func selectBtn(_ sender: UIButton) {

   }

This worked for me. I changed the return type from "Any" to "UIButton"

Upvotes: 0

Sean
Sean

Reputation: 249

If you change the sender to AnyObject as opposed to Any or UIButton it will allow you to connect multiple buttons to the same action.

Upvotes: 13

assangar
assangar

Reputation: 105

If you change "Any" to "UIButton" it will work.

Upvotes: 4

Mayur Bansal
Mayur Bansal

Reputation: 3

IN Swift 3 full version this will work.

Just change Any to UIButton and you can connect more button actions to this particular method.

So after changing it should looks like this :

@IBAction func s(_ sender: UIButton) { } Sure this will solve the issue. :)

Upvotes: 0

Anil Singh
Anil Singh

Reputation: 4501

I have changed the sender to UIButton from Any

Control drag the button to the name of the function.

Upvotes: 0

AloSwift
AloSwift

Reputation: 407

When you connect a button action, by default the method will looks like :

@IBAction func s(_ sender: Any) {
    }

Just change Any to UIButton and you can connect more button actions to this particular method.

So after changing it should looks like this :

@IBAction func s(_ sender: UIButton) {
    }

Hope this will solve the issue. :)

Upvotes: 10

Mudith Chathuranga Silva
Mudith Chathuranga Silva

Reputation: 7434

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! :)

Upvotes: 18

Frederic Adda
Frederic Adda

Reputation: 6092

I guess it's another issue : if you try to create an IBAction the first time, it works fine. But if for any reason you decide to re-connect the IBAction (same action to another object, or rename the function), then it fails.

In fact, you create an IBAction called doSomething, which creates a function func doSomething(sender: UIButton). But with XCode 8 (or Swift 2.3 / 3.0), this method is interpreted as func doSomethingWithSender(sender: UIButton).

I guess it's a bug, Apple went a bit too hard on the "rename Objc-methods" thing.

Upvotes: 0

Caleb Kleveter
Caleb Kleveter

Reputation: 11494

If I am correct you need to use UIGestureRecognizer to attach multiple things to one action.

What you could do is place a UIGestureRecognizer on each button, and then setup one of them like you normally would with a button (ctrl-drag), than ctrl-drag from the other gesture recognizers to the first action you made.

Upvotes: 1

Related Questions