0

BaseView という名前の UIView のサブクラスがあります。BaseView のサブクラスで、いくつかのコードで didSet を作成します。UIViewController では、BaseView のこのサブクラスを初期化しますが、彼は didSet を呼び出しません

BaseView コード:

class BaseView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    func setupViews() { }

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

BaseView コードのサブクラス:

class DetailProductView: BaseView {

var product: Product? {
    didSet {
        productImage.image = UIImage(named: (product?.productImageName)!)
        productTitle.text = product?.title
        productCompositionLabel.text = product?.description
        productPriceLabel.text = "₽" + product!.productPrice!.stringValue
        productWeightLabel.text = product!.productWeight!.stringValue + "г."
    }
}

UIViewController コード:

class DetailProductController: UIViewController {

    var product: Product?

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white

        let productView = DetailProductView(frame: self.view.bounds)
        view.addSubview(productView)
        view.layoutSubviews()
    }
}
4

1 に答える 1