在swift中覆盖UIView的init方法 [英] 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)
如果用户不想初始化自己的属性,我想使用默认属性初始化 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.
这篇关于在swift中覆盖UIView的init方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!