Can not connect custom protocol delegate from storyboard in Xcode 6.1

二次信任 提交于 2019-12-12 09:49:50

问题


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 {

...

}

回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/26603571/can-not-connect-custom-protocol-delegate-from-storyboard-in-xcode-6-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!