我正在尝试将 UIView 类的委托(delegate)返回给另一个类,但没有成功。
我有一个名为 Class1 的自定义 UIView
子类,我在其中添加了另一个自定义 UIView
子类,它有一个委托(delegate)。
在第 1 类中:
func setModuleViewFor(index1:Int,index2:Int, delegating:UIView)
{
let tmodule = ModuleInput(frame: CGRect(x: 0,y: 0,width: self.frame.size.width,height: self.frame.size.height))
tmodule.setWithData(index1: index1,index2: index2)
tmodule.delegate= delegating //********error
self.addSubview(tmodule)
这给出了错误的说法:
can not assign value of type UIView to ModuleInputProtocol
通常,如果 ModuleInput
在控制 View 中,我会这样做:
tmodule.delegate= self
有效。
最佳答案
您必须使用 ModuleInputProtocol 设置委托(delegate)类型
func setModuleViewFor(index1:Int,index2:Int, delegating:ModuleInputProtocol)
{
let tmodule = ModuleInput(frame: CGRect(x: 0,y: 0,width: self.frame.size.width,height: self.frame.size.height))
tmodule.setWithData(index1: index1,index2: index2)
tmodule.delegate= delegating //********error
self.addSubview(tmodule)
调用setModuleViewFor方法时委托(delegate)参数必须实现ModuleInputProtocol
add:protocol protocolBase {
}
protocol protocol0:protocolBase {
}
protocol protocol1:protocolBase {
}
protocol protocol2:protocolBase {
}
protocol protocol3:protocolBase {
}
protocol protocol4:protocolBase {
}
现在你可以这样写你的函数了
func setModuleViewFor(index1:Int,index2:Int, delegating:protocolBase)
我的英文很烂,请谅解
关于ios - 通过函数返回委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39974187/