UIView instanceFromNib函数

问题描述

我正在尝试展示我在xib文件中内置的自定义键盘,当我实例化该类时,我需要将一些变量传递给该类,以使事情正常运行,但是却出现了此错误Instance member 'target' cannot be used on type 'CustomKeyboardView'

在线

 self.target = target
 self.section = section


class CustomKeyboardView: UIView {
    
    weak var target:UIKeyInput?
    var section:Int?
    
    class func instanceFromNib(target: UIKeyInput,section: Int) -> UIView {
        self.target = target
        self.section = section
        return UINib(nibName: "CustomKeyboard",bundle: nil).instantiate(withOwner: nil,options: nil)[0] as! UIView
    }
}

如何解决此问题或以其他方式将信息传递给班级?

解决方法

您尝试将值设置为类类型而不是实例。首先从笔尖获取键盘,然后设置其属性并返回:

class func instanceFromNib(target : UIKeyInput,section : Int) -> UIView {
    let keyboard = UINib(nibName: ...
    keyboard,target = target
    keyboard.section = section
    return keyboard
}