1

我知道必须调用超类的指定初始化器,我认为init(type: UIButtonType)已经调用了指定初始化器,所以我在子类便利初始化器中使用它,但失败了

class TSContourButton: UIButton {
enum ContourButtonSizeType {
    case large
    case small
}

convenience init(type:ContourButtonSizeType) {
    self.init(type: .custom)
}

然后,我尝试了这个。它编译好了。但是,看起来不专业

class TSClass: UIButton {

convenience init(frame: CGRect, myString: String) {
    self.init(frame: frame)
    self.init(type: .custom)
}

所以,我怀疑我可能想错了。所以,我做了一些测试。它成功调用super convenience initializer。为什么我不能self.init(type: .custom)在我的子类中使用便利初始化程序UIButton

class person: UIButton {
var name: String = "test"

override init(frame: CGRect) {
    super.init(frame: .zero)
    self.name = "one"
}

convenience init(myName: String) {
    self.init(frame: .zero)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

class man: person {
convenience init(mySex: Int) { // it successfully call superclass convenience initializer
    self.init(myName: "info")
}
4

2 回答 2

1

init(type: UIButtonType)不是UIButton的指定初始化器,init(frame: CGRect)是UIButton的右指定初始化器

你只需要覆盖init(frame: CGRect)

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let button1 = MyButton(type: .custom)
    }

}

class MyButton: UIButton {

    // 初始化父类的指定构造器,然后你就可以获得父类的便利构造器
    // overwrite the designated initializer of the super class, then you can automatically inherit the convenience initializer of the super class
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

如果要自定义自己的按钮,可以添加convenience init(myType: UIButton.ButtonType)

    class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let button1 = MyButton(type: .custom)
        let button2 = MyButton(myType: .custom)
    }

}


class MyButton: UIButton {

    // 初始化父类的指定构造器,然后你就可以获得父类的便利构造器
    // overwrite the designated initializer of the super class, then you can automatically inherit the convenience initializer of the super class
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    // 创建自己的遍历构造器
    // this is what you want
    convenience init(myType: UIButton.ButtonType) {
        self.init(type: myType) // called the convenience initializer which you automatically inherit from super class
        // customize your own button
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

如果对你有用,可以给个赞吗,(づ ̄3 ̄)づ</p>

于 2018-12-12T06:53:30.433 回答
1

例如,如果 name 是您的必填字段,那么您将在一个函数中实现所有初始设置。如果name不可用,您应该处理。small如果没有提供类型,我将保留为默认选项。

// MARK:- Designated Initializers
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    initialSetup(type: .small)
}

override init(frame: CGRect) {
    super.init(frame: frame)

    initialSetup(type: .small)
}

// MARK:- Convenience Initializers
convenience init(type: ContourButtonSizeType) {
    self.init(frame: .zero)

    initialSetup(type: type)
}

func initialSetup(type: ContourButtonSizeType) {
    // handle all initial setup 
}
于 2017-01-04T07:53:31.087 回答