4

I just upgraded my mac to 10.10 and Xcode to 6.1,

found a strange thing which about storyboard,

my case is using a swift project, can not connect custom protocol delegate from storyboard anymore.

the old connected which comes with old version of Xcode is fine, but I can not connect any new delegate anymore.

even I can not reconnect the old one once I removed the connected.

Does anyone occur this situation ??

============================== Updated ==============================

View Class

@objc public protocol VideoViewResizeDelegate {

    func shouldVideoViewResetLayout(videoView: GvVideoView) -> Bool;

}

@IBOutlet var resizeDelegate: VideoViewResizeDelegate?;

ViewController Class

@IBDesignable public class ViewController: UIViewController, VideoViewResizeDelegate {

...

}
Malloc
  • 15,434
  • 34
  • 105
  • 192
PatrickSCLin
  • 1,419
  • 3
  • 17
  • 45
  • Can you show us the code/storyboard? It sounds like the subclass is not set on the view controller you are trying to connect. – Fogmeister Oct 28 '14 at 08:40
  • I can connect the delegate by storyboard in Xcode 6.0, it was the same code, can not connect anymore once I remove the connected – PatrickSCLin Oct 28 '14 at 08:48
  • 1
    Possible duplicate of [Interface Builder, @IBOutlet and protocols for delegate and dataSource in Swift](http://stackoverflow.com/questions/26180268/interface-builder-iboutlet-and-protocols-for-delegate-and-datasource-in-swift) – Lars Blumberg Apr 05 '16 at 08:34

2 Answers2

14

https://developer.apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051

Interface Builder

Interface Builder does not support connecting to an outlet in a Swift file when the outlet’s type is a protocol. Declare the outlet's type as AnyObject or NSObject, connect objects to the outlet using Interface Builder, then change the outlet's type back to the protocol. (17023935)

it's sucks...

PatrickSCLin
  • 1,419
  • 3
  • 17
  • 45
  • 3
    And in 2017 ... it is still the case. I don't get where they're going with all this @ Apple. Swift compiler taking huge amounts of time to compile some simple statements, incremental compilation bugs causing rebuilds of all your files for small changes, compiler crashes, inconsistent compiler error messages, interface builder problems, and ZERO refactoring support in Xcode. Yeah, this is really the language of the future for everything Apple related ... – Joris Mans Jan 02 '17 at 21:15
8

It's 2017....

Using swift 3, this will work:

open class YourClass: UIView {

    #if TARGET_INTERFACE_BUILDER
    @IBOutlet open weak var delegate: AnyObject?
    #else
    open weak var delegate: YourClassDelegate?
    #endif

}

A precondition is:

YourClassDelegate must be decorated with @objc

For example:

@objc public protocol MyDelegate: class {
    func myFunc()
    func myFunc2()
}

Update

This should be fixed in Xcode 9

Yilei He
  • 149
  • 2
  • 7
  • 1
    Hey Tom, I tested it in Xcode 8.3, and Swift 3.1, it works for me. By the way, there is a precondition: your delegate protocol must be decorated by @objc – Yilei He May 08 '17 at 06:30
  • Wow.. Thanks man.. it works for me.. I was looking for this... There must be a way and here you gave it to me. Thanks – Abdul Yasin Jul 31 '17 at 07:37