我试图继承UIButton,但我希望它的类型是.system。我正在努力与初始值设定项

class FormButton: UIButton {

var type FormButtonType: FormButtomType.oneSelection

init(oftype formType: FormButtomType) {
    self.type = formType
    super.init(type: .system)
}

}

问题是我收到以下错误消息:“必须调用父类(super class)'UIButton'的指定初始化程序

最佳答案

您不能覆盖便捷方法并调用 super 便捷方法...
或者,您可以执行一个静态方法来返回FormButton类型的UIButtonType.system

class FormButton: UIButton {
    class func newButton() -> FormButton {
        return FormButton.init(type: .system)
    }
}

这样使用
let button = FormButton.newButton()

08-27 07:59