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

让人想犯罪 __ 提交于 2020-01-10 22:30:16

问题


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.


回答1:


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

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




回答2:


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.




回答3:


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




回答4:


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




回答5:


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.




回答6:


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
   }

}



回答7:


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.




回答8:


I have changed the sender to UIButton from Any

Control drag the button to the name of the function.




回答9:


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




回答10:


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"



来源:https://stackoverflow.com/questions/37845914/xcode8-cannot-connect-multiple-single-uibuttons-to-single-ibaction-in-swift-fil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!