Interface Builder,@ IBOutlet和Swift中的delegate和dataSource协议 [英] Interface Builder, @IBOutlet and protocols for delegate and dataSource in Swift

查看:77
本文介绍了Interface Builder,@ IBOutlet和Swift中的delegate和dataSource协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法将声明为@IBOutlet的 CustomView 的委托属性连接到Interface Builder中的 ViewController - 只需'建立连接。

Can't connect delegate property of CustomView declared as @IBOutlet toViewController in Interface Builder – simply can't establish a connection.

这是代码

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

@objc protocol CustomViewDelegate {
     ...
}


class ViewController: UIViewController, CustomViewDelegate {
     ...
}

@objc 是因为 swift protocol,IBOutlet属性不能有非对象类型,不知道为什么协议CustomViewDelegate:class {} 不起作用。

@objc is used because of swift protocol, IBOutlet property cannot have non-object type, don't know why protocol CustomViewDelegate: class {} doesn't work.

其他人遇到过类似的事情吗?

Anyone else came across something like that?

推荐答案

来自Xcode发行说明:

From the Xcode release notes:


当插座的类型是协议时,Interface Builder不支持连接到Swift文件中的插座。

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

解决方法:将插座的类型声明为AnyObject或NSObject,使用Interface Builder将对象连接到插座,然后将插座的类型更改回pr协议。

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发行说明说不再需要这种解决方法。

Xcode 9 beta 3 release notes say that this workaround should no longer be necessary.

这篇关于Interface Builder,@ IBOutlet和Swift中的delegate和dataSource协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆