gpt4 book ai didi

swift - Swift 中的 Interface Builder、@IBOutlet 和委托(delegate)和数据源协议(protocol)

转载 作者:IT王子 更新时间:2023-10-29 05:05:31 26 4
gpt4 key购买 nike

无法将声明为 @IBOutlet 的 CustomView 的委托(delegate)属性连接到 Interface Builder 中的 ViewController – 根本无法建立连接。

这是代码

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

@objc protocol CustomViewDelegate {
...
}


class ViewController: UIViewController, CustomViewDelegate {
...
}

@objc 是因为 swift protocol, IBOutlet property cannot have non-object type 使用的,不知道为什么 protocol 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.

编辑:Xcode 9 beta 3 发行说明说不再需要此解决方法。

关于swift - Swift 中的 Interface Builder、@IBOutlet 和委托(delegate)和数据源协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26180268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com