Designated Initializer of UITextView

喜你入骨 提交于 2020-01-21 11:19:48

问题


When I create a new subclass of UITextView in the Xcode 6 Beta, the following code is automatically provided.

import UIKit

class TerminalView: UITextView {

    init(frame: CGRect) {
        super.init(frame: frame)
        // Initialization code
    }
}

The previous code (completely provided by Xcode with nothing removed) gives the following error.

Must call a designated initializer of the superclass 'UITextView'

As far as I know, the designated for all subclasses of UIView is -initWithFrame: (or in Swift, init(frame:). If this is the case, why does the code provided by Xcode result in an error? I have added no new instance variables to the class, so nothing else has to be initialized yet.


回答1:


It seems as though the only initializer that works for now is:

super.init(frame: CGRect, textContainer: NSTextContainer?)

which can be called with

super.init(frame: CGRect.zero, textContainer: nil)

This is most likely a bug in the initial beta release and will be fixed in upcoming beta releases.




回答2:


For 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
    }
}


来源:https://stackoverflow.com/questions/24074955/designated-initializer-of-uitextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!