Can't connect IBAction in Xcode

前端 未结 12 1558

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

相关标签:
12条回答
  • 2021-02-02 13:08

    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

    0 讨论(0)
  • 2021-02-02 13:09

    My solution was to delete the UIButton from the .xib file and to add it again. Looks like something strange is happening in the .xib file, readding it should be a quick fix.

    0 讨论(0)
  • 2021-02-02 13:11

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

    It might work.

    0 讨论(0)
  • 2021-02-02 13:14

    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 { }
    
    0 讨论(0)
  • 2021-02-02 13:17

    Make sure you select Automatic.

    enter image description here

    0 讨论(0)
  • 2021-02-02 13:17

    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

    0 讨论(0)
提交回复
热议问题