我正在项目中创建MKAnnotationView
的子类。它需要有两个属性来存储子视图,我需要在某个地方初始化这些属性。
MKAnnotationView
在其文档中列出了一个初始化器initWithAnnotation:reuseIdentifier:
,因此我想我可以简单地重写它:
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
override init!(annotation: MKAnnotation!, reuseIdentifier: String!) {
innerCircle = ...
outerCircle = ...
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
...
}
但这会导致运行时异常:
致命错误:类“PulsatingDotMarker”使用未实现的初始化程序“init(frame:)”
好的,我想initWithAnnotation:reuseIdentifier:
内部调用了initWithFrame:
,所以我应该覆盖后者。让我们尝试一下:
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
override init(frame: CGRect) {
innerCircle = ...
outerCircle = ...
super.init(frame: frame)
}
...
}
但是,这会在创建注释视图对象时引发编译错误:
调用中存在“reuseIdentifier”额外参数
如果我实现了(必需的)初始化程序initWithFrame:
,那么默认的初始化程序initWithAnnotation:reuseIdentifier:
就会丢失吗?
也许如果我添加一个重载initWithAnnotation:reuseIdentifier:
的方法,只需调用super
,那它就会再次可用,这样行吗?
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
init!(annotation: MKAnnotation!, reuseIdentifier: String!) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
override init(frame: CGRect) {
innerCircle = ...
outerCircle = ...
super.init(frame: frame)
}
...
}
不行,仍然不好——编译错误:
在 super.init 调用时未初始化属性 'self.innerCircle'
好的,如果我有一个 initWithFrame:
方法,但是在 initWithAnnotation:reuseIdentifier:
中初始化了子视图呢?(但那么如果有人直接调用 initWithFrame:
呢?...)
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
init!(annotation: MKAnnotation!, reuseIdentifier: String!) {
innerCircle = ...
outerCircle = ...
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
...
}
毫不意外,Swift通过告诉我以下信息来保护我:
在super.init调用时,属性“self.innerCircle”没有被初始化
(这次是在initWithFrame:
中)。
那么我应该怎么办呢?我不能在这里和那里都创建子视图,对吗?
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
init!(annotation: MKAnnotation!, reuseIdentifier: String!) {
innerCircle = ...
outerCircle = ...
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
override init(frame: CGRect) {
innerCircle = ...
outerCircle = ...
super.init(frame: frame)
}
...
}
我又错了,但实际上这个可以工作-尽管我在同一个对象中两次分配了一个常量属性(!)。
应该如何正确地完成这个任务?
(注意: 该类还包括一个必需的initWithCoder:
初始化程序,在第一个示例中只是调用fatalError
,但从 storyboard 中永远不会创建该对象。)
var innerCircle: UIView!
会怎样?这可能避免“required init(xxx)”错误。 - user467105[self initWith ...]
方法),但不幸的是目前还没有解决方案 - https://dev59.com/sYzda4cB1Zd3GeqPn42a - Rupert