ios - 通过函数返回委托(delegate)?

标签 ios swift

我正在尝试将 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/

相关文章:

ios - MKMapView 改变当前位置飞艇颜色

swift - 值为 "_type_type"是什么意思?

swift - 如何更改标签中某些单词的颜色 - Swift 3

ios - 在 UIImagePickerController 上放置滑动手势

ios - 单击时删除 UITableViewCells 按钮

swift - 如何在SpriteKit中制作移动障碍物

ios - 内部带有 UIImageView 的子类 UIView 不响应自动布局

php - 将照片从iPhone上传到Web服务器

ios - 如何阅读plist

javascript - 如何使用js以编程方式为android/ios上的网站创建桌面快捷方式?