快速覆盖 UIView 的 init 方法 [英] Override init method of UIView in swift

查看:22
本文介绍了快速覆盖 UIView 的 init 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)

如果用户不想初始化自己的属性,我想用默认属性初始化CustomLoadingView...

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:

必须调用超类 UIView 的指定初始化器

Must call a designated intializer of the superclass UIView

推荐答案

init(subviewColor: UIColor, subViewMessage: String) 中,您没有调用指定的初始化程序(正如编译器指出的那样)很好).

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.

在这种情况下,UIView 的指定初始化器是 init(frame: CGRect),这意味着在某些时候,你的新初始化器 init(subviewColor: UIColor, subViewMessage: String 必须调用super.init(frame:).

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

}

至于使用简单的 CustomLoadingView() 的便捷方法,您必须为此添加另一个初始化程序.将此代码添加到您的自定义视图:

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.

这篇关于快速覆盖 UIView 的 init 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆