简体   繁体   中英

Override init method of UIView in swift

class CustomView: UIView {

    var subViewColor:UIColor
    var subViewMessage:String

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

    init(subViewColor:UIColor,subViewMessage:String){

        self.subViewColor = subViewColor
        self.subViewMessage = subViewMessage
        super.init()

    }

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

}

I have a class where I want the user to initialize a custom view either by giving properties like:

let myView = CustomLoadingView(initialize properties here)

If the user does not want to initialize their own properties, I want to initialize CustomLoadingView using default properties...

let myView = CustomLoadingView() // this should initialize using default value

However, with this, I am getting this error:

Must call a designated intializer of the superclass UIView

In init(subviewColor: UIColor, subViewMessage: String) , you aren't calling the designated initializer (as the compiler points out nicely).

If you don't know what designated initializers are, they are initializers that have to be called by the subclass at some point. From the docs:

Designated initializers are the primary initializers for a class. A designated initializer fully initializes all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up the superclass chain.

In this case, the designated initializer for UIView is init(frame: CGRect) , meaning at some point, your new initializer init(subviewColor: UIColor, subViewMessage: String must call super.init(frame:) .

In order to fix this, make the following changes:

init(frame: CGRect, subViewColor: UIColor, subViewMessage: String){

    self.subViewColor = subViewColor
    self.subViewMessage = subViewMessage
    super.init(frame: frame)

}

OR you can call your other initializer in your class which ends up calling the designated initializer.

override init(frame: CGRect) {
    super.init(frame: frame) // calls designated initializer
}

convenience init(frame: CGRect, subViewColor: UIColor, subViewMessage: String){

    self.subViewColor = subViewColor
    self.subViewMessage = subViewMessage
    self.init(frame: frame) // calls the initializer above

}

As for the convenience method with simply CustomLoadingView() , you have to add another initializer for that. Add this code to your custom view:

convenience init() {
    self.init(frame: DEFAULT_FRAME, subViewColor: DEFAULT_COLOR, subViewMessage: DEFAULT_MESSAGE)
}

If you want to learn more about designated and convenience initializers, read about them here and here .

Try this:

class CustomView: UIView {

    var subViewColor:UIColor
    var subViewMessage:String

    init(subViewColor:UIColor,subViewMessage:String){

        self.subViewColor = subViewColor
        self.subViewMessage = subViewMessage

        let frame = self.frame
        //Or you can use custom frame. 

        super.init(frame: frame)

    }

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

You must call one of UIView's designated initializers at some point in your custom initializer, for instance: super.init(frame: frameX) . Your call of super.init() does not satisfy this requirement.

对不起,它正在从Bundle.main.loadNibNamed("XibNameThatDoesntExist", owner: self, options: nil)自定义视图初始化程序中调用一个不存在的Bundle.main.loadNibNamed("XibNameThatDoesntExist", owner: self, options: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM