Can not connect custom protocol delegate from storyboard in Xcode 6.1

大城市里の小女人 提交于 2019-12-06 01:13:11

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

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

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