簡體   English   中英

`override init(frame:CGRect)`從未調用過?

[英]`override init(frame: CGRect)` never called?

我有一個簡單的UIView子類,如下所示:

import UIKit

class AngleViewManager: UIView {

    class AngleView: UIView {
        override init(frame: CGRect) {
            super.init(frame: frame)
            backgroundColor = UIColor.red
        }


        init(first: CGPoint, second: CGPoint, third: CGPoint) {
            // setup
            super.init(frame: CGRect(dimensions))
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
}

當我在AngleViewManager初始化AngleView實例時

AngleView(firstPoint, secondPoint, thirdPoint)

背景顏色不是紅色,即使在我成功自定義的自定義初始化程序中顯式調用了它,在覆蓋的init中設置一個斷點也表明它從未被調用。

我是否缺少明顯的東西?

您正在調用init(frame: CGRect)的超類( UIView )實現。

只需將其更改為self.init(frame: frame)

暫無
暫無

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

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