iosswiftuibuttoninterface-builderprotocol-extension

UIButton stop showing UIControl actions in interface builder after adding extension to it


I have the simplest UI - just one button:enter image description here

As you can see, I can drag any event (like UITouchUpInside) from interface builder to my code.

BUT if I add some extension to UIButton like:

protocol SomeProtocol {}

extension UIButton: SomeProtocol {}

those events no longer visible: enter image description here

I'm using Xcode version 8.2 (8C38)

Can I somehow return them? Is it a bug in Xcode?


Solution

  • It seems an Xcode bug.

    A workaround you could try is to extend UIControl with the protocol. UIControl is the superclass of UIButton, so extensions to UIControl are inherited by UIButton, and Xcode doesn't seem to be upset when a protocol is adopted by UIControl.

    protocol SomeProtocol { }
    
    extension UIControl: SomeProtocol { }
    

    From Apple Developer Forums

    Adding a Swift protocol to UIButton breaks Interface Builder action options.