Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not connect custom protocol delegate from storyboard in Xcode 6.1

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 {

...

}
like image 674
PatrickSCLin Avatar asked Nov 29 '22 00:11

PatrickSCLin


2 Answers

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

like image 88
PatrickSCLin Avatar answered Dec 05 '22 07:12

PatrickSCLin


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

like image 34
Yilei He Avatar answered Dec 05 '22 06:12

Yilei He