Interface Builder,@IBOutlet和Swift中委托和dataSource的协议

前端之家收集整理的这篇文章主要介绍了Interface Builder,@IBOutlet和Swift中委托和dataSource的协议前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在Interface Builder中无法将CustomView的委托属性声明为@IBOutlet toViewController – 根本无法建立连接。

这是代码

class CustomView: UIView {
     @IBOutlet var delegate: CustomViewDelegate?
}

@objc protocol CustomViewDelegate {
     ...
}


class ViewController: UIViewController,CustomViewDelegate {
     ...
}

使用@objc因为swift protocol,IBOutlet property cannot have non-object type,不知道为什么协议CustomViewDelegate:class {}不起作用。

还有人碰到过这样的东西吗?

@H_301_10@
从Xcode发行说明:

Interface Builder does not support connecting to an outlet in a Swift file when the outlet’s type is a protocol.

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

原文链接:https://www.f2er.com/swift/320629.html

猜你在找的Swift相关文章