Why is this View nil in awakeFromNib()? [duplicate]

若如初见. 提交于 2019-12-11 08:27:41

问题


I'm practicing awakeFromNib.

I have a single UIView in Main.storyboard. This UIView inherit class CardView.

Codes are below

class ViewController: UIViewController {

    @IBOutlet weak var cardView: CardView!

}

And I have a CardView.xib. In, CardView.xib, there's a default single UIView which inherits a class CardView. And inside this UIView, there's the other single view which inherits a class CardContentView. And Of course, I have a CardView.swift which have class CardView.

Codes are below

class CardView: UIView {

    @IBOutlet weak var cardContentView: CardContentView!

    override func awakeFromNib() {
        cardContentView.layer.cornerRadius = 16
    }
}

And I have a CardContentView.xib. In, CardContentView.xib, there's a default single UIView which inherits a default class UIView. And Of course, I have a CardContentView.swift which have class CardContentView.

Codes are below

class CardContentView: UIView {

    @IBOutlet var backgroundView: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    private func commonInit(){
        Bundle.main.loadNibNamed("CardContentView", owner: self, options: nil)
        self.addSubview(backgroundView)
    }

}

Yes, I want to show the CardContentView to cardView in ViewController through class CardView.

But when I run, the error pops in class CardView

The error line is on the func awakeFromNib

The specific line is cardContentView.layer.cornerRadius = 16.

The error message is Unexpectedly found nil while unwrapping an Optional value. Especially, cardContentView = nil.

I don't know why cardContentView = nil.

I linked cardContentView in Xib board to class CardView in CardView.swift.

And how should I modify the code to run this?

Thank you!


回答1:


First of all, you need to connect the cardView outlet in the main storyboard, and of course, you've added a view in your ViewController. Then in your CardView class, you have to instantiate cardContentView outlet, because in CardView.xib you do not have any reference to cardContentView. Make sure that you've connected backgroundView outlet in CardContentView.xib.

class CardView: UIView {

    @IBOutlet weak var cardContentView: CardContentView!

    override func awakeFromNib() {
        super.awakeFromNib()
        cardContentView = Bundle.main.loadNibNamed("CardContentView", owner: self, options: nil)!.first as! CardContentView
        self.addSubview(cardContentView)
        cardContentView.layer.cornerRadius = 16

        //you can also get access to the subviews of cardContentView
        //cardContentView.backgroundView.backgroundColor = .red
    }
}


class CardContentView: UIView {

    @IBOutlet var backgroundView: UIView!

    override func awakeFromNib() {
        backgroundView.backgroundColor = .yellow
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        //commonInit()
    } 

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        //commonInit()
    }

    private func commonInit() {
    }
}


来源:https://stackoverflow.com/questions/52911734/why-is-this-view-nil-in-awakefromnib

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