簡體   English   中英

UITextView 的指定初始化器

[英]Designated Initializer of UITextView

當我在 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 提供的代碼會導致錯誤?我添加了 no類的新實例變量,因此無需初始化任何其他內容。

似乎現在唯一有效的初始化程序是:

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

可以用

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

這很可能是初始 Beta 版中的一個錯誤,並將在即將發布的 Beta 版中修復。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM