Home » Ios » Can't connect IBAction in Xcode

Can't connect IBAction in Xcode

Posted by: admin February 24, 2018 Leave a comment

Questions:

When I drag from a button on the storyboard to my view controller Swift file, I only get the option to insert an Outlet or Outlet Collection.

Similarly, if I create the IBAction manually and try to connect it to a button on the storyboard, I can’t connect the two.

This problem is only appears in one of my projects, but it happens on all the storyboards in my project.

Seems like an Xcode bug, but maybe someone knows of a workaround.

(It’s hard to tell, but the element being dragged in the screenshot is a button.)

enter image description here

Answers:

I have very similar issue in my Swift project that was driving me mad.

It turned that’s it is the problem of mixing custom protocol adoption in extension of UIButton having @IBInspectable property. More details below.

I finally found out that’s because I have following code in my project:
(copyright goes to Kevin McNeigh: https://www.iphonelife.com/blog/31369/swift-programming-101-mastering-dynamic-type-ios)

protocol DynamicTypeChangeHandler {

    var typeObserver: Bool {get set}
}

extension UIButton: DynamicTypeChangeHandler {
    @IBInspectable var typeObserver: Bool {
        // ...
    }
}

If I remove either protocol adoption from the extension or @IBInspectable keyword, I can again setup actions in Interface Builder. Sheer lunacy. Xcode <3

Questions:
Answers:

You can try to change the @IBAction‘s argument from (_ sender: Any) to (_ sender: UIButton) or (_ sender: AnyObject), but manually.

It might work.

Questions:
Answers:

I can’t speak to the underlying cause–something always seems to be breaking in XCode, but you can always write your IBAction manually, @IBAction func(sender: UIButton) { } in your view/controller and then control-drag from the storyboard element to the action itself. If that doesn’t work try linking from the node created by an IBAction in the margin of your code back to the storyboard. This works for me when XCode stops working.

Questions:
Answers:

Make sure you select Automatic.

enter image description here

Questions:
Answers:

This bug is indeed super crazy. I lost hours because of it.
Until now I found 2 reasons, and I’ll post here maybe somebody will read and be helpful:

  1. If anybody is using BonMot pod, this is one possible cause. If you remove it, the IBAction will be back.

  2. Any extension on UIButton or UIBarButtonItem can cause this as well. Comment the extension, and the IBAction will be back. Happy coding.


UPDATE: BonMot fixed this issue in 4.0.2

Questions:
Answers:

This apparently is a known issue on the Apple forums, but I don’t know if Apple is working on it:

https://forums.developer.apple.com/message/216258

Here is what I did for a workaround.

  1. Go into my Podfile and comment out the pod library (in my case, ReactiveKit Bond) that is doing an extension causing the issue.
  2. Do a pod update to remove the pod from the project.
  3. Go back in and do my IBAction connections in IB like I normally would. Do as many together as possible for efficiency.
  4. Finally go back and uncomment your pod library and do another pod update to reimport the pods.
  5. Test by setting a breakpoint in your new IBAction functions to verify your connections are working.
Questions:
Answers:

For me, I was trying to connect multiple buttons to one IBAction, none of the solutions worked until I switched my method signature to specify that the sender was a UIButton rather than Any

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

Questions:
Answers:

If extension is causing the problem, then instead of directly extending UIButton, user typealias e.g, create extension of CustomButton instead of UIButton

typealias CustomButton = UIButton
extension CustomButton { }

Questions:
Answers:

I was having the same issue but it was because I was trying to connect an ImageView to an IBAction (forgive me, not sure if my terminology is correct). I deleted the ImageView object and replaced it with a Button and
IBAction then became an option rather than just IBOutlet and Outlet Collection.

Questions:
Answers:

For me, it was a FUIButton (custom from SAP Fiori) causing same issue. No pods or UIButton extension specified in my own code (in addition to SAP UIButton obviously). Cmd + Alt + Shift + K, DerivedData Clean didn’t help either.
@IBAction wasn’t available in Interface Builder till I changed my Button Class back to simple UIButton.

And… voila! ta-da!