UIView instanceFromNib() 函数

UIView instanceFromNib() function

我正在尝试展示我在 xib 文件中构建的自定义键盘,当我实例化 class 时,我需要将一些变量传递给 class 以使事情正常工作,但我收到此错误: 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?

您尝试将值设置为 class 类型而不是实例。首先从 nib 获取键盘然后设置它的属性和 return 它 :

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