在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 {}不起作用。
还有人碰到过这样的东西吗?
从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.