ios 快速覆盖 UIView 的 init 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35496962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Override init method of UIView in swift
提问by tktsubota
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...
如果用户不想初始化自己的属性,我想CustomLoadingView
使用默认属性进行初始化...
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
必须调用超类 UIView 的指定初始化器
回答by tktsubota
In init(subviewColor: UIColor, subViewMessage: String)
, you aren't calling the designated initializer (as the compiler points out nicely).
在 中init(subviewColor: UIColor, subViewMessage: String)
,您没有调用指定的初始化程序(正如编译器很好地指出的那样)。
If you don't know what designated initializers are, they are initializers that haveto 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:)
.
在这种情况下,指定的初始化程序UIView
是init(frame: CGRect)
,这意味着在某些时候,您的新初始化程序init(subviewColor: UIColor, subViewMessage: String
必须调用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:
至于使用 simple 的便捷方法CustomLoadingView()
,您必须为此添加另一个初始化程序。将此代码添加到您的自定义视图:
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 hereand here.
回答by TechBee
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")
}
}
回答by sakurasunris3
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.
你必须调用的UIView的指定初始化的一个在您的自定义初始化一些点,例如:super.init(frame: frameX)
。您的调用super.init()
不满足此要求。
回答by Michal Shatz
Foe me it was calling a xib that doesn't exist from custom view initialiser in
Bundle.main.loadNibNamed("XibNameThatDoesntExist", owner: self, options: nil)
`
敌人我这是叫不从自定义视图初始化剂中存在的厦门国际银行
Bundle.main.loadNibNamed("XibNameThatDoesntExist", owner: self, options: nil)
`