当我在Xcode 6 Beta中创建UITextView
的新子类时,会自动提供以下代码。
import UIKit
class TerminalView: UITextView {
init(frame: CGRect) {
super.init(frame: frame)
// Initialization code
}
}
前面的代码(完全由Xcode提供,没有删除任何内容)给出以下错误:
Must call a designated initializer of the superclass 'UITextView'
据我所知,UIView
的所有子类都指定为-initWithFrame:
(Swift中为init(frame:)
)。如果是这样的话,为什么Xcode提供的代码会导致错误?我没有向类中添加新的实例变量,所以还不需要初始化其他变量
似乎目前唯一有效的初始化式是:
super.init(frame: CGRect, textContainer: NSTextContainer?)
可以用
调用super.init(frame: CGRect.zero, textContainer: nil)
这很可能是最初测试版中的一个错误,将在即将发布的测试版中修复。
2020:
class SpecialText: UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
common()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
common()
}
private func common() {
backgroundColor = .yellow
font = .systemFont(ofSize: 26)
textColor = .green
}
}